<!DOCTYPE html>
<html lang="en-US">
    <head>
        <title>Math::Matrix - multiply and invert matrices - metacpan.org</title>
        <link rel="preload" as="fetch" href="https://metacpan.org/account/login_status" crossorigin="anonymous" />
        <link href="https://metacpan.org/_assets/b8ccceeed47a0652049703d99326a9cea4933443.css" rel="stylesheet" type="text/css">
        <script src="https://metacpan.org/_assets/6bfedafe2d7caa915b7d84f61b45936818e3242e.js" type="text/javascript" defer></script>
        <link rel="alternate" type="application/rss+xml" title="Recent CPAN Uploads of Math-Matrix - MetaCPAN" href="https://metacpan.org/dist/Math-Matrix/releases.rss" />
        <link rel="canonical" href="./Math::Matrix.html" />
        <meta name="description" content="multiply and invert matrices" />
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5">
        <link rel="shortcut icon" href="https://metacpan.org/static/icons/favicon.ico">
        <link rel="apple-touch-icon" sizes="152x152" href="https://metacpan.org/static/icons/apple-touch-icon.png">
        <link rel="search" href="https://metacpan.org/static/opensearch.xml" type="application/opensearchdescription+xml" title="MetaCPAN">
        <script>
          (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
          (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
          })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

          (function(skey, ga_id){
            ga('create', ga_id, {
              siteSpeedSampleRate : 100,
              storage             : 'none',
              clientId            : localStorage.getItem(skey)
            });
            ga(function(tracker) {
              localStorage.setItem(skey, tracker.get('clientId'));
            });
            ga('send', 'pageview');
          })('ga:clientId', 'UA-27829474-1');
        </script>
<meta name="twitter:card"        content="summary" />
<meta name="twitter:url"         content="https://metacpan.org/pod/Math::Matrix" />
<meta name="twitter:title"       content="Math::Matrix" />
<meta name="twitter:description" content="multiply and invert matrices" />
<meta name="twitter:site"        content="metacpan" />
    </head>
    <body>
        <nav class="navbar navbar-default" role="navigation">
            <div class="header-logo-large hidden-xs">
              <a href="https://metacpan.org/" tabindex="0">
                <svg class="logo" aria-label="MetaCPAN">
                  <use class="logo" href="/static/images/metacpan-logo.svg#logo" />
                </svg>
              </a>
            </div>
            <div class="header-logo-icon visible-xs">
              <a href="https://metacpan.org/">
                <svg class="logo" aria-label="MetaCPAN">
                  <use class="logo" href="/static/images/metacpan-logo.svg#dots" />
                </svg>
              </a>
            </div>
            <ul class="nav navbar-nav menu-items hidden-xs hidden-sm">
              <li><a href="https://metacpan.org/about">About</a></li>
              <li><a href="https://metacpan.org/about/sponsors">Sponsor</a></li>
              <li><a href="https://grep.metacpan.org/">grep::cpan</a></li>
              <li><a href="https://metacpan.org/recent">Recent</a></li>
              <li><a href="https://metacpan.org/about/faq">FAQ</a></li>
              <li><a href="https://metacpan.org/tools">Tools</a></li>
              <li><a href="https://fastapi.metacpan.org/">API</a></li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <button type="button" class="searchbar-btn visible-xs visible-sm">
                    <i class="fa fa-search button-fa-icon"></i>
                </button>
                <form action="https://metacpan.org/search" class="searchbar-form visible-md visible-lg search-form form-horizontal">
                   <input type="hidden" name="size" id="metacpan_search-size" value="20">
                  <div class="form-group">
                      <div class="search-group">
                        <i class="fa fa-search"></i>
                        <input type="text" name="q" placeholder="Search the CPAN" size="41" autocorrect="off" autocapitalize="off" spellcheck="false" id="metacpan_search-input" class="form-control" value="">
                      </div>
                  </div>
                </form>
                    <li class="icon-slidepanel visible-xs visible-sm">
                      <button data-toggle="slidepanel" data-target=".slidepanel">
                        <span class="button-fa-icon">
                          <i class="fa fa-bars slidepanel-open"></i>
                          <i class="fa fa-times slidepanel-close"></i>
                        </span>
                      </button>
                    </li>
                <form action="https://metacpan.org/account/logout" method="POST" id="metacpan-logout"></form>
                <li class="dropdown logged_in" style="display: none;">
                    <button type="button" class="dropdown-toggle" data-toggle="dropdown">
                      <i class="fa fa-user button-fa-icon logged-in-icon" aria-hidden="true"></i>
                      <i class="fas fa-chevron-down"></i>
                    </button>
                    <ul class="dropdown-menu">
                        <li><a href="https://metacpan.org/account/identities">Identities</a></li>
                        <li><a href="https://metacpan.org/account/profile">Profile</a></li>
                        <li><a href="https://metacpan.org/account/favorite/list">Favorites</a></li>
                        <li>
                            <a href="./Math::Matrix.html#" type="button" onclick="$('#metacpan-logout').submit(); return false">
                              Logout
                            </a>
                        </li>
                    </ul>
                </li>
                <li class="dropdown logged_out" style="display: none;">
                    <button type="button" class="dropdown-toggle" data-toggle="dropdown">
                      <i class="fa fa-user button-fa-icon" aria-hidden="true"></i>
                      <i class="fas fa-chevron-down"></i>
                    </button>
                    <ul class="dropdown-menu">
                        <li>
                            <a href="https://metacpan.org/login/github">
                                <i class="fab fa-github fa-fw"></i>
                                GitHub
                            </a>
                        </li>
                        <li>
                            <a href="https://metacpan.org/login/twitter">
                                <i class="fab fa-twitter fa-fw"></i>
                                Twitter
                            </a>
                        </li>
                        <li>
                            <a href="https://metacpan.org/login/google">
                                <i class="fab fa-google fa-fw"></i>
                                Google
                            </a>
                        </li>
                    </ul>
                </li>
                <li class="dropdown logged_placeholder">
                    <button>
                      <i class="fa fa-user button-fa-icon" aria-hidden="true"></i>
                    </button>
                </li>
            </ul>
        </nav>
        <div class="page-content ">
          <!--
          <div class="top-notify-banner">
            <i class="fas fa-info-circle"></i>
          </div>
          -->
          <nav class="sidebar">
            <div class="slidepanel">
              <ul class="nav-list ">
    <li class="nav-header no-margin-top">
      <div class="ttip" data-toggle="tooltip" data-placement="bottom" title="The date that this version of Math-Matrix was released.">
      <span class="relatize">13 Jan 2021 13:22:59 UTC</span>
    </li>
  <li>
    Distribution: <a href="https://metacpan.org/dist/Math-Matrix">Math-Matrix</a>
  </li>
  <li>
    Module version: 0.94
  </li>
  <li>
    <a data-keyboard-shortcut="g s" href="https://metacpan.org/dist/Math-Matrix/source/lib/Math/Matrix.pm">Source</a>
    (<a href="https://metacpan.org/dist/Math-Matrix/source/lib/Math/Matrix.pm?raw=1">raw</a>)
  </li>
  <li>
    <a data-keyboard-shortcut="g b" href="https://metacpan.org/dist/Math-Matrix/source/lib/Math">Browse</a>
    (<a href="https://metacpan.org/dist/Math-Matrix/source/lib/Math?raw=1">raw</a>)
  </li>
    <li>
      <a data-keyboard-shortcut="g c" href="https://metacpan.org/dist/Math-Matrix/changes">Changes</a>
    </li>
    <li>
      <a class="nopopup" href="https://metacpan.org/dist/Math-Matrix/contribute">How to Contribute</a>
    </li>
    <li>
        <a rel="noopener nofollow" data-keyboard-shortcut="g r" href="https://github.com/pjacklam/p5-Math-Matrix">Repository</a>
    </li>
    <li>
      <a rel="noopener nofollow" data-keyboard-shortcut="g i" href="https://rt.cpan.org/Public/Dist/Display.html?Name=Math-Matrix">Issues</a>
      (1)
    </li>
    <li>
      <a rel="noopener nofollow" href="http://matrix.cpantesters.org/?dist=Math-Matrix+0.94" title="Matrix">Testers</a>
        <span title="(pass / fail / na)">(<a rel="noopener nofollow" href="https://www.cpantesters.org/distro/M/Math-Matrix.html?oncpan=1&amp;distmat=1&amp;version=0.94&amp;grade=2" style="color: #090">1455</a> / <a rel="noopener nofollow" href="https://www.cpantesters.org/distro/M/Math-Matrix.html?oncpan=1&amp;distmat=1&amp;version=0.94&amp;grade=3" style="color: #900">1</a> / <a rel="noopener nofollow" href="https://www.cpantesters.org/distro/M/Math-Matrix.html?oncpan=1&amp;distmat=1&amp;version=0.94&amp;grade=4">0</a>)</span>
    </li>
    <li>
      <a rel="noopener nofollow" href="http://cpants.cpanauthors.org/release/PJACKLAM/Math-Matrix-0.94">Kwalitee</a>
    </li>
    <li>
      <div class="ttip" data-toggle="tooltip" data-placement="bottom" title="The # people with an indexing permission on Math-Matrix who have released something to CPAN in the last 2 years (i.e. the # people likely able to release critical fixes in a timely manner)">
      Bus factor: 1
      </div>
    </li>
    <li>
      <a rel="noopener nofollow" href="http://cpancover.com/latest/Math-Matrix-0.94/index.html">80.83% Coverage </a>
    </li>
    <li>
      License: perl_5
    </li>
    <li class="nav-header">Activity</li>
    <li>
<div class="activity-graph">
    <img src="https://metacpan.org/dist/Math-Matrix/activity.svg?res=month" />
    <div class="comment">24 month</div>
</div>
    </li>
    <li class="nav-header">Tools</li>
    <li>
      <a itemprop="downloadUrl" href="https://cpan.metacpan.org/authors/id/P/PJ/PJACKLAM/Math-Matrix-0.94.tar.gz">
      Download (<span itemprop="fileSize">110.15KB</span>)</a>
    </li>
    <li>
      <a href="https://explorer.metacpan.org/?url=%2Fmodule%2FPJACKLAM%2FMath-Matrix-0.94%2Flib%2FMath%2FMatrix.pm">
        MetaCPAN Explorer
      </a>
    </li>
    <li>
      <a href="https://metacpan.org/dist/Math-Matrix/permissions">
        Permissions
      </a>
    </li>
    <li>
      <a href="https://metacpan.org/dist/Math-Matrix/releases.rss">
        Subscribe to distribution
      </a>
    </li>
    <li>
      <button class="btn btn-link" data-toggle="modal" data-target="#metacpan_install-instructions-dialog">
        Install Instructions
      </button>
    </li>
    <li>
      <form action="https://metacpan.org/search">
        <input type="hidden" name="q" value="dist:Math-Matrix">
        <input type="search" name="q" placeholder="Search distribution" class="form-control tool-bar-form">
        <input type="submit" style="display: none">
      </form>
    </li>
    <li>
      <form action="https://grep.metacpan.org/search">
        <input type="hidden" name="qd" value="Math-Matrix">
        <input type="hidden" name="source" value="metacpan">
        <input type="search" name="q" placeholder="grep distribution" class="form-control tool-bar-form">
        <input type="submit" style="display: none">
     </form>
    </li>
    <li class="version-jump">
<select onchange="document.location.href=&#39;/release/&#39;+this.value+&#39;/view/lib/Math/Matrix.pm&#39;" class="form-control tool-bar-form">
  <option disabled selected>Jump to version</option>
<option
  disabled
  value="PJACKLAM/Math-Matrix-0.94"
>0.94
  (PJACKLAM on 2021-01-13)</option>
<option
  
  value="PJACKLAM/Math-Matrix-0.93"
>0.93
  (PJACKLAM on 2021-01-12)</option>
<option
  
  value="ULPFR/Math-Matrix-0.8"
>0.8
  (ULPFR on 2013-09-30)</option>
<option
  
  value="ULPFR/Math-Matrix-0.7"
>0.7
  (ULPFR on 2013-05-18)</option>
<option
  
  value="ULPFR/Math-Matrix-0.6"
>0.6
  (ULPFR on 2013-05-17)</option>
<option
  
  value="ULPFR/Math-Matrix-0.5"
>0.5
  (ULPFR on 2006-03-19)</option>
<option
  
  value="ULPFR/Math-Matrix-0.4"
>0.4
  (ULPFR on 2001-09-24)</option>
<option
  
  value="ULPFR/Math-Matrix-0.3"
>0.3
  (ULPFR on 2001-04-17)</option>
<option
  
  value="ULPFR/Math-Matrix-0.2"
>0.2
  (ULPFR on 1996-07-10)</option>
<option
  
  value="ULPFR/Math-Matrix-0.1"
>0.1
  (ULPFR on 1995-10-27)</option>
<optgroup label="BackPAN">'
<option
  
  value="PJACKLAM/Math-Matrix-0.92"
>0.92
  (PJACKLAM on 2020-11-08)</option>
<option
  
  value="PJACKLAM/Math-Matrix-0.91"
>0.91
  (PJACKLAM on 2020-08-10)</option>
<option
  
  value="PJACKLAM/Math-Matrix-0.9"
>0.9
  (PJACKLAM on 2020-08-07)</option>
</optgroup>
</select>
    </li>
    <li class="version-diff">
<select onchange="document.location.href='/release/PJACKLAM/Math-Matrix-0.94/diff/' + encodeURIComponent(this.value) + '/lib/Math/Matrix.pm'
" class="form-control tool-bar-form">
  <option disabled selected>Diff with version</option>
<option
  disabled
  value="PJACKLAM/Math-Matrix-0.94"
>0.94
  (PJACKLAM on 2021-01-13)</option>
<option
  
  value="PJACKLAM/Math-Matrix-0.93"
>0.93
  (PJACKLAM on 2021-01-12)</option>
<option
  
  value="ULPFR/Math-Matrix-0.8"
>0.8
  (ULPFR on 2013-09-30)</option>
<option
  
  value="ULPFR/Math-Matrix-0.7"
>0.7
  (ULPFR on 2013-05-18)</option>
<option
  
  value="ULPFR/Math-Matrix-0.6"
>0.6
  (ULPFR on 2013-05-17)</option>
<option
  
  value="ULPFR/Math-Matrix-0.5"
>0.5
  (ULPFR on 2006-03-19)</option>
<option
  
  value="ULPFR/Math-Matrix-0.4"
>0.4
  (ULPFR on 2001-09-24)</option>
<option
  
  value="ULPFR/Math-Matrix-0.3"
>0.3
  (ULPFR on 2001-04-17)</option>
<option
  
  value="ULPFR/Math-Matrix-0.2"
>0.2
  (ULPFR on 1996-07-10)</option>
<option
  
  value="ULPFR/Math-Matrix-0.1"
>0.1
  (ULPFR on 1995-10-27)</option>
<optgroup label="BackPAN">'
<option
  
  value="PJACKLAM/Math-Matrix-0.92"
>0.92
  (PJACKLAM on 2020-11-08)</option>
<option
  
  value="PJACKLAM/Math-Matrix-0.91"
>0.91
  (PJACKLAM on 2020-08-10)</option>
<option
  
  value="PJACKLAM/Math-Matrix-0.9"
>0.9
  (PJACKLAM on 2020-08-07)</option>
</optgroup>
</select>
    </li>

    <li>
<ul class="dependencies">
  <li class="nav-header">Dependencies</li>
  <li><a href="https://metacpan.org/pod/Carp" title="Carp" class="ellipsis">Carp</a></li>
  <li><a href="https://metacpan.org/pod/Math::Trig" title="Math::Trig" class="ellipsis">Math::Trig</a></li>
  <li><a href="https://metacpan.org/pod/Scalar::Util" title="Scalar::Util" class="ellipsis">Scalar::Util</a></li>
  <li><i class="ttip" title="dynamic_config enabled">and possibly others</i></li>
  <li>
    <hr>
  </li>
  <li>
    <a href="https://metacpan.org/module/Math::Matrix/requires">Reverse dependencies</a>
  </li>
  <li>
    <a href="http://deps.cpantesters.org/?module=Math%3A%3AMatrix">CPAN Testers List</a>
  </li>
  <li>
    <a href="https://cpandeps.grinnz.com/?dist=Math-Matrix">Dependency graph</a>
  </li>
</ul>
    </li>
    <li class="nav-header">Permalinks</li>
    <li>
      <a href="https://metacpan.org/release/PJACKLAM/Math-Matrix-0.94/view/lib/Math/Matrix.pm">This version</a>
    </li>
    <li>
      <a href="./Math::Matrix.html">Latest version</a>
    </li>
    <li>
<div class="plussers">
<div class="nav-header">++ed by:</div>
<div>
<a class="display-all" href="https://metacpan.org/author/BRTASTIC"><img src="https://www.gravatar.com/avatar/ee4e20f351921e70f1985c5623e709e5?d=identicon&amp;s=20" title="BRTASTIC" alt="BRTASTIC"></a>
</div>
<!-- Display counts of plussers-->
<div>
    <a href="https://metacpan.org/dist/Math-Matrix/plussers">1 PAUSE user</a>
</div>
<div>
    1 non-PAUSE user
</div>
</div>
    </li>
    <li>
    </li>
              </ul>
            </div>
          </nav>
          <div class="content-navigation">
<div class="breadcrumbs">
  <span>
    <a data-keyboard-shortcut="g a" rel="author" href="https://metacpan.org/author/PJACKLAM" class="author-name">Peter John Acklam</a>
  </span>
  <span>&nbsp;/&nbsp;</span>
  <div class="release dist-release status-latest maturity-released">
    <span class="dropdown"><b class="caret"></b></span>
<select onchange="document.location.href=&#39;/release/&#39;+this.value+&#39;/view/lib/Math/Matrix.pm&#39;" class="">
<option
  selected
  value="PJACKLAM/Math-Matrix-0.94"
>0.94
  (PJACKLAM on 2021-01-13)</option>
<option
  
  value="PJACKLAM/Math-Matrix-0.93"
>0.93
  (PJACKLAM on 2021-01-12)</option>
<option
  
  value="ULPFR/Math-Matrix-0.8"
>0.8
  (ULPFR on 2013-09-30)</option>
<option
  
  value="ULPFR/Math-Matrix-0.7"
>0.7
  (ULPFR on 2013-05-18)</option>
<option
  
  value="ULPFR/Math-Matrix-0.6"
>0.6
  (ULPFR on 2013-05-17)</option>
<option
  
  value="ULPFR/Math-Matrix-0.5"
>0.5
  (ULPFR on 2006-03-19)</option>
<option
  
  value="ULPFR/Math-Matrix-0.4"
>0.4
  (ULPFR on 2001-09-24)</option>
<option
  
  value="ULPFR/Math-Matrix-0.3"
>0.3
  (ULPFR on 2001-04-17)</option>
<option
  
  value="ULPFR/Math-Matrix-0.2"
>0.2
  (ULPFR on 1996-07-10)</option>
<option
  
  value="ULPFR/Math-Matrix-0.1"
>0.1
  (ULPFR on 1995-10-27)</option>
<optgroup label="BackPAN">'
<option
  
  value="PJACKLAM/Math-Matrix-0.92"
>0.92
  (PJACKLAM on 2020-11-08)</option>
<option
  
  value="PJACKLAM/Math-Matrix-0.91"
>0.91
  (PJACKLAM on 2020-08-10)</option>
<option
  
  value="PJACKLAM/Math-Matrix-0.9"
>0.9
  (PJACKLAM on 2020-08-07)</option>
</optgroup>
</select>
    <a data-keyboard-shortcut="g d" class="release-name" href="https://metacpan.org/dist/Math-Matrix">Math-Matrix-0.94</a>
  </div>
<span class="river-gauge-gauge">
  <svg width="24px"
       height="15px"
       version="1.1"
       xmlns="http://www.w3.org/2000/svg"
       xmlns:xlink="http://www.w3.org/1999/xlink">

    <g>
      <title>        River stage one &#10;
          • 5 direct dependents &#10;          • 7 total dependents
      </title>

      <rect x="0"  y="0" width="4" height="15" fill="#7ea3f2" />
      <rect x="5"  y="0" width="4" height="15" fill="#e4e2e2" />
      <rect x="10"  y="0" width="4" height="15" fill="#e4e2e2" />
      <rect x="15"  y="0" width="4" height="15" fill="#e4e2e2" />
      <rect x="20"  y="0" width="4" height="15" fill="#e4e2e2" />
    </g>
  </svg>

</span>
<div id="Math-Matrix-fav" class="logged_in">
<form action="https://metacpan.org/account/favorite/add" style="display: inline" onsubmit="return favDistribution(this)">
    <input type="hidden" name="remove" value="0">
    <input type="hidden" name="release" value="Math-Matrix-0.94">
    <input type="hidden" name="author" value="PJACKLAM">
    <input type="hidden" name="distribution" value="Math-Matrix">
    <button type="submit" class="favorite highlight"><span>2</span> ++</button>
</form>
</div>
<div class="logged_out">
<a href="./Math::Matrix.html" onclick="alert('Please sign in to add favorites'); return false" class="favorite highlight">
<span>2</span> ++</a>
</div>
   / <span>Math::Matrix</span>
</div>
          </div>
          <main class="content">


<nav class="toc">
  <div class="toc-header"><strong>Contents</strong></div>
<ul>
  <li><a href="./Math::Matrix.html#NAME">NAME</a></li>
  <li><a href="./Math::Matrix.html#SYNOPSIS">SYNOPSIS</a></li>
  <li><a href="./Math::Matrix.html#DESCRIPTION">DESCRIPTION</a></li>
  <li><a href="./Math::Matrix.html#METHODS">METHODS</a>
    <ul>
      <li><a href="./Math::Matrix.html#Constructors">Constructors</a></li>
      <li><a href="./Math::Matrix.html#Identify-matrices">Identify matrices</a></li>
      <li><a href="./Math::Matrix.html#Identify-elements">Identify elements</a></li>
      <li><a href="./Math::Matrix.html#Basic-properties">Basic properties</a></li>
      <li><a href="./Math::Matrix.html#Manipulate-matrices">Manipulate matrices</a></li>
      <li><a href="./Math::Matrix.html#Mathematical-functions">Mathematical functions</a>
        <ul>
          <li><a href="./Math::Matrix.html#Addition">Addition</a></li>
          <li><a href="./Math::Matrix.html#Subtraction">Subtraction</a></li>
          <li><a href="./Math::Matrix.html#Negation">Negation</a></li>
          <li><a href="./Math::Matrix.html#Multiplication">Multiplication</a></li>
          <li><a href="./Math::Matrix.html#Powers">Powers</a></li>
          <li><a href="./Math::Matrix.html#Inversion">Inversion</a></li>
          <li><a href="./Math::Matrix.html#Factorisation">Factorisation</a></li>
          <li><a href="./Math::Matrix.html#Miscellaneous-matrix-functions">Miscellaneous matrix functions</a></li>
          <li><a href="./Math::Matrix.html#Elementwise-mathematical-functions">Elementwise mathematical functions</a></li>
          <li><a href="./Math::Matrix.html#Columnwise-or-rowwise-mathematical-functions">Columnwise or rowwise mathematical functions</a></li>
        </ul>
      </li>
      <li><a href="./Math::Matrix.html#Comparison">Comparison</a>
        <ul>
          <li><a href="./Math::Matrix.html#Matrix-comparison">Matrix comparison</a></li>
          <li><a href="./Math::Matrix.html#Scalar-comparison">Scalar comparison</a></li>
        </ul>
      </li>
      <li><a href="./Math::Matrix.html#Vector-functions">Vector functions</a></li>
      <li><a href="./Math::Matrix.html#Conversion">Conversion</a></li>
      <li><a href="./Math::Matrix.html#Matrix-utilities">Matrix utilities</a>
        <ul>
          <li><a href="./Math::Matrix.html#Apply-a-subroutine-to-each-element">Apply a subroutine to each element</a></li>
          <li><a href="./Math::Matrix.html#Forward-elimination">Forward elimination</a></li>
          <li><a href="./Math::Matrix.html#Back-substitution">Back-substitution</a></li>
        </ul>
      </li>
      <li><a href="./Math::Matrix.html#Miscellaneous-methods">Miscellaneous methods</a></li>
    </ul>
  </li>
  <li><a href="./Math::Matrix.html#OVERLOADING">OVERLOADING</a></li>
  <li><a href="./Math::Matrix.html#IMPROVING-THE-SOLUTION-OF-LINEAR-SYSTEMS">IMPROVING THE SOLUTION OF LINEAR SYSTEMS</a>
    <ul>
      <li><a href="./Math::Matrix.html#Background">Background</a></li>
      <li><a href="./Math::Matrix.html#Stopping-criteria">Stopping criteria</a></li>
      <li><a href="./Math::Matrix.html#Parameters">Parameters</a></li>
      <li><a href="./Math::Matrix.html#Example">Example</a></li>
    </ul>
  </li>
  <li><a href="./Math::Matrix.html#SUBCLASSING">SUBCLASSING</a></li>
  <li><a href="./Math::Matrix.html#BUGS">BUGS</a></li>
  <li><a href="./Math::Matrix.html#SUPPORT">SUPPORT</a></li>
  <li><a href="./Math::Matrix.html#LICENSE-AND-COPYRIGHT">LICENSE AND COPYRIGHT</a></li>
  <li><a href="./Math::Matrix.html#AUTHORS">AUTHORS</a></li>
</ul></nav>
<div class="pod anchors">
<h1 id="NAME">NAME</h1>

<p>Math::Matrix - multiply and invert matrices</p>

<h1 id="SYNOPSIS">SYNOPSIS</h1>

<pre><code>    use Math::Matrix;

    # Generate a random 3-by-3 matrix.
    srand(time);
    my $A = Math::Matrix -&gt; new([rand, rand, rand],
                                [rand, rand, rand],
                                [rand, rand, rand]);
    $A -&gt; print(&quot;A\n&quot;);

    # Append a fourth column to $A.
    my $x = Math::Matrix -&gt; new([rand, rand, rand]);
    my $E = $A -&gt; concat($x -&gt; transpose);
    $E -&gt; print(&quot;Equation system\n&quot;);

    # Compute the solution.
    my $s = $E -&gt; solve;
    $s -&gt; print(&quot;Solutions s\n&quot;);

    # Verify that the solution equals $x.
    $A -&gt; multiply($s) -&gt; print(&quot;A*s\n&quot;);</code></pre>

<h1 id="DESCRIPTION">DESCRIPTION</h1>

<p>This module implements various constructors and methods for creating and manipulating matrices.</p>

<p>All methods return new objects, so, for example, <code>$X-&gt;add($Y)</code> does not modify <code>$X</code>.</p>

<pre><code>    $X -&gt; add($Y);         # $X not modified; output is lost
    $X = $X -&gt; add($Y);    # this works</code></pre>

<p>Some operators are overloaded (see <a href="./Math::Matrix.html#OVERLOADING">&quot;OVERLOADING&quot;</a>) and allow the operand to be modified directly.</p>

<pre><code>    $X = $X + $Y;          # this works
    $X += $Y;              # so does this</code></pre>

<h1 id="METHODS">METHODS</h1>

<h2 id="Constructors">Constructors</h2>

<dl>

<dt id="new()"><a id="new"></a>new()</dt>
<dd>

<p>Creates a new object from the input arguments and returns it.</p>

<p>If a single input argument is given, and that argument is a reference to array whose first element is itself a reference to an array, it is assumed that the argument contains the whole matrix, like this:</p>

<pre><code>    $x = Math::Matrix-&gt;new([[1, 2, 3], [4, 5, 6]]); # 2-by-3 matrix
    $x = Math::Matrix-&gt;new([[1, 2, 3]]);            # 1-by-3 matrix
    $x = Math::Matrix-&gt;new([[1], [2], [3]]);        # 3-by-1 matrix</code></pre>

<p>If a single input argument is given, and that argument is not a reference to an array, a 1-by-1 matrix is returned.</p>

<pre><code>    $x = Math::Matrix-&gt;new(1);                      # 1-by-1 matrix</code></pre>

<p>Note that all the folling cases result in an empty matrix:</p>

<pre><code>    $x = Math::Matrix-&gt;new([[], [], []]);
    $x = Math::Matrix-&gt;new([[]]);
    $x = Math::Matrix-&gt;new([]);</code></pre>

<p>If <code><a href="./Math::Matrix.html#new()">&quot;new()&quot;</a></code> is called as an instance method with no input arguments, a zero filled matrix with identical dimensions is returned:</p>

<pre><code>    $b = $a-&gt;new();     # $b is a zero matrix with the size of $a</code></pre>

<p>Each row must contain the same number of elements.</p>

</dd>
<dt id="new_from_sub()"><a id="new_from_sub"></a>new_from_sub()</dt>
<dd>

<p>Creates a new matrix object by doing a subroutine call to create each element.</p>

<pre><code>    $sub = sub { ... };
    $x = Math::Matrix -&gt; new_from_sub($sub);          # 1-by-1
    $x = Math::Matrix -&gt; new_from_sub($sub, $m);      # $m-by-$m
    $x = Math::Matrix -&gt; new_from_sub($sub, $m, $n);  # $m-by-$n</code></pre>

<p>The subroutine is called in scalar context with two input arguments, the row and column indices of the element to be created. Note that no checks are performed on the output of the subroutine.</p>

<p>Example 1, a 4-by-4 identity matrix can be created with</p>

<pre><code>    $sub = sub { $_[0] == $_[1] ? 1 : 0 };
    $x = Math::Matrix -&gt; new_from_sub($sub, 4);</code></pre>

<p>Example 2, the code</p>

<pre><code>    $x = Math::Matrix -&gt; new_from_sub(sub { 2**$_[1] }, 1, 11);</code></pre>

<p>creates the following 1-by-11 vector with powers of two</p>

<pre><code>    [ 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 ]</code></pre>

<p>Example 3, the code, using <code>$i</code> and <code>$j</code> for increased readability</p>

<pre><code>    $sub = sub {
        ($i, $j) = @_;
        $d = $j - $i;
        return $d == -1 ? 5
             : $d ==  0 ? 6
             : $d ==  1 ? 7
             : 0;
    };
    $x = Math::Matrix -&gt; new_from_sub($sub, 5);</code></pre>

<p>creates the tridiagonal matrix</p>

<pre><code>    [ 6 7 0 0 0 ]
    [ 5 6 7 0 0 ]
    [ 0 5 6 7 0 ]
    [ 0 0 5 6 7 ]
    [ 0 0 0 5 6 ]</code></pre>

</dd>
<dt id="new_from_rows()"><a id="new_from_rows"></a>new_from_rows()</dt>
<dd>

<p>Creates a new matrix by assuming each argument is a row vector.</p>

<pre><code>    $x = Math::Matrix -&gt; new_from_rows($y, $z, ...);</code></pre>

<p>For example</p>

<pre><code>    $x = Math::Matrix -&gt; new_from_rows([1, 2, 3],[4, 5, 6]);</code></pre>

<p>returns the matrix</p>

<pre><code>    [ 1 2 3 ]
    [ 4 5 6 ]</code></pre>

</dd>
<dt id="new_from_cols()"><a id="new_from_cols"></a>new_from_cols()</dt>
<dd>

<p>Creates a matrix by assuming each argument is a column vector.</p>

<pre><code>    $x = Math::Matrix -&gt; new_from_cols($y, $z, ...);</code></pre>

<p>For example,</p>

<pre><code>    $x = Math::Matrix -&gt; new_from_cols([1, 2, 3],[4, 5, 6]);</code></pre>

<p>returns the matrix</p>

<pre><code>    [ 1 4 ]
    [ 2 5 ]
    [ 3 6 ]</code></pre>

</dd>
<dt id="id()"><a id="id"></a>id()</dt>
<dd>

<p>Returns a new identity matrix.</p>

<pre><code>    $I = Math::Matrix -&gt; id($n);    # $n-by-$n identity matrix
    $I = $x -&gt; id($n);              # $n-by-$n identity matrix
    $I = $x -&gt; id();                # identity matrix with size of $x</code></pre>

</dd>
<dt id="new_identity()"><a id="new_identity"></a>new_identity()</dt>
<dd>

<p>This is an alias for <code><a href="./Math::Matrix.html#id()">&quot;id()&quot;</a></code>.</p>

</dd>
<dt id="eye()"><a id="eye"></a>eye()</dt>
<dd>

<p>This is an alias for <code><a href="./Math::Matrix.html#id()">&quot;id()&quot;</a></code>.</p>

</dd>
<dt id="exchg()"><a id="exchg"></a>exchg()</dt>
<dd>

<p>Exchange matrix.</p>

<pre><code>    $x = Math::Matrix -&gt; exchg($n);     # $n-by-$n exchange matrix</code></pre>

</dd>
<dt id="scalar()"><a id="scalar"></a>scalar()</dt>
<dd>

<p>Returns a scalar matrix, i.e., a diagonal matrix with all the diagonal elements set to the same value.</p>

<pre><code>    # Create an $m-by-$m scalar matrix where each element is $c.
    $x = Math::Matrix -&gt; scalar($c, $m);

    # Create an $m-by-$n scalar matrix where each element is $c.
    $x = Math::Matrix -&gt; scalar($c, $m, $n);</code></pre>

<p>Multiplying a matrix A by a scalar matrix B is effectively the same as multiply each element in A by the constant on the diagonal of B.</p>

</dd>
<dt id="zeros()"><a id="zeros"></a>zeros()</dt>
<dd>

<p>Create a zero matrix.</p>

<pre><code>    # Create an $m-by-$m matrix where each element is 0.
    $x = Math::Matrix -&gt; zeros($m);

    # Create an $m-by-$n matrix where each element is 0.
    $x = Math::Matrix -&gt; zeros($m, $n);</code></pre>

</dd>
<dt id="ones()"><a id="ones"></a>ones()</dt>
<dd>

<p>Create a matrix of ones.</p>

<pre><code>    # Create an $m-by-$m matrix where each element is 1.
    $x = Math::Matrix -&gt; ones($m);

    # Create an $m-by-$n matrix where each element is 1.
    $x = Math::Matrix -&gt; ones($m, $n);</code></pre>

</dd>
<dt id="inf()"><a id="inf"></a>inf()</dt>
<dd>

<p>Create a matrix of positive infinities.</p>

<pre><code>    # Create an $m-by-$m matrix where each element is Inf.
    $x = Math::Matrix -&gt; inf($m);

    # Create an $m-by-$n matrix where each element is Inf.
    $x = Math::Matrix -&gt; inf($m, $n);</code></pre>

</dd>
<dt id="nan()"><a id="nan"></a>nan()</dt>
<dd>

<p>Create a matrix of NaNs (Not-a-Number).</p>

<pre><code>    # Create an $m-by-$m matrix where each element is NaN.
    $x = Math::Matrix -&gt; nan($m);

    # Create an $m-by-$n matrix where each element is NaN.
    $x = Math::Matrix -&gt; nan($m, $n);</code></pre>

</dd>
<dt id="constant()"><a id="constant"></a>constant()</dt>
<dd>

<p>Returns a constant matrix, i.e., a matrix whose elements all have the same value.</p>

<pre><code>    # Create an $m-by-$m matrix where each element is $c.
    $x = Math::Matrix -&gt; constant($c, $m);

    # Create an $m-by-$n matrix where each element is $c.
    $x = Math::Matrix -&gt; constant($c, $m, $n);</code></pre>

</dd>
<dt id="rand()"><a id="rand"></a>rand()</dt>
<dd>

<p>Returns a matrix of uniformly distributed random numbers in the range [0,1).</p>

<pre><code>    $x = Math::Matrix -&gt; rand($m);          # $m-by-$m matrix
    $x = Math::Matrix -&gt; rand($m, $n);      # $m-by-$n matrix</code></pre>

<p>To generate an <code>$m</code>-by-<code>$n</code> matrix of uniformly distributed random numbers in the range [0,<code>$a</code>), use</p>

<pre><code>    $x = $a * Math::Matrix -&gt; rand($m, $n);</code></pre>

<p>To generate an <code>$m</code>-by-<code>$n</code> matrix of uniformly distributed random numbers in the range [<code>$a</code>,<code>$b</code>), use</p>

<pre><code>    $x = $a + ($b - $a) * Math::Matrix -&gt; rand($m, $n);</code></pre>

<p>See also <code><a href="./Math::Matrix.html#randi()">&quot;randi()&quot;</a></code> and <code><a href="./Math::Matrix.html#randn()">&quot;randn()&quot;</a></code>.</p>

</dd>
<dt id="randi()"><a id="randi"></a>randi()</dt>
<dd>

<p>Returns a matrix of uniformly distributed random integers.</p>

<pre><code>    $x = Math::Matrix -&gt; randi($max);                 # 1-by-1 matrix
    $x = Math::Matrix -&gt; randi($max, $n);             # $n-by-$n matrix
    $x = Math::Matrix -&gt; randi($max, $m, $n);         # $m-by-$n matrix

    $x = Math::Matrix -&gt; randi([$min, $max]);         # 1-by-1 matrix
    $x = Math::Matrix -&gt; randi([$min, $max], $n);     # $n-by-$n matrix
    $x = Math::Matrix -&gt; randi([$min, $max], $m, $n); # $m-by-$n matrix</code></pre>

<p>See also <code><a href="./Math::Matrix.html#rand()">&quot;rand()&quot;</a></code> and <code><a href="./Math::Matrix.html#randn()">&quot;randn()&quot;</a></code>.</p>

</dd>
<dt id="randn()"><a id="randn"></a>randn()</dt>
<dd>

<p>Returns a matrix of random numbers from the standard normal distribution.</p>

<pre><code>    $x = Math::Matrix -&gt; randn($m);         # $m-by-$m matrix
    $x = Math::Matrix -&gt; randn($m, $n);     # $m-by-$n matrix</code></pre>

<p>To generate an <code>$m</code>-by-<code>$n</code> matrix with mean <code>$mu</code> and standard deviation <code>$sigma</code>, use</p>

<pre><code>    $x = $mu + $sigma * Math::Matrix -&gt; randn($m, $n);</code></pre>

<p>See also <code><a href="./Math::Matrix.html#rand()">&quot;rand()&quot;</a></code> and <code><a href="./Math::Matrix.html#randi()">&quot;randi()&quot;</a></code>.</p>

</dd>
<dt id="clone()"><a id="clone"></a>clone()</dt>
<dd>

<p>Clones a matrix and returns the clone.</p>

<pre><code>    $b = $a-&gt;clone;</code></pre>

</dd>
<dt id="diagonal()"><a id="diagonal"></a>diagonal()</dt>
<dd>

<p>A constructor method that creates a diagonal matrix from a single list or array of numbers.</p>

<pre><code>    $p = Math::Matrix-&gt;diagonal(1, 4, 4, 8);
    $q = Math::Matrix-&gt;diagonal([1, 4, 4, 8]);</code></pre>

<p>The matrix is zero filled except for the diagonal members, which take the values of the vector.</p>

<p>The method returns <b>undef</b> in case of error.</p>

</dd>
<dt id="tridiagonal()"><a id="tridiagonal"></a>tridiagonal()</dt>
<dd>

<p>A constructor method that creates a matrix from vectors of numbers.</p>

<pre><code>    $p = Math::Matrix-&gt;tridiagonal([1, 4, 4, 8]);
    $q = Math::Matrix-&gt;tridiagonal([1, 4, 4, 8], [9, 12, 15]);
    $r = Math::Matrix-&gt;tridiagonal([1, 4, 4, 8], [9, 12, 15], [4, 3, 2]);</code></pre>

<p>In the first case, the main diagonal takes the values of the vector, while both of the upper and lower diagonals&#39;s values are all set to one.</p>

<p>In the second case, the main diagonal takes the values of the first vector, while the upper and lower diagonals are each set to the values of the second vector.</p>

<p>In the third case, the main diagonal takes the values of the first vector, while the upper diagonal is set to the values of the second vector, and the lower diagonal is set to the values of the third vector.</p>

<p>The method returns <b>undef</b> in case of error.</p>

</dd>
<dt id="blkdiag()"><a id="blkdiag"></a>blkdiag()</dt>
<dd>

<p>Create block diagonal matrix. Returns a block diagonal matrix given a list of matrices.</p>

<pre><code>    $z = Math::Matrix -&gt; blkdiag($x, $y, ...);</code></pre>

</dd>
</dl>

<h2 id="Identify-matrices"><a id="Identify"></a>Identify matrices</h2>

<dl>

<dt id="is_empty()"><a id="is_empty"></a>is_empty()</dt>
<dd>

<p>Returns 1 is the invocand is empty, i.e., it has no elements.</p>

<pre><code>    $bool = $x -&gt; is_empty();</code></pre>

</dd>
<dt id="is_scalar()"><a id="is_scalar"></a>is_scalar()</dt>
<dd>

<p>Returns 1 is the invocand is a scalar, i.e., it has one element.</p>

<pre><code>    $bool = $x -&gt; is_scalar();</code></pre>

</dd>
<dt id="is_vector()"><a id="is_vector"></a>is_vector()</dt>
<dd>

<p>Returns 1 is the invocand is a vector, i.e., a row vector or a column vector.</p>

<pre><code>    $bool = $x -&gt; is_vector();</code></pre>

</dd>
<dt id="is_row()"><a id="is_row"></a>is_row()</dt>
<dd>

<p>Returns 1 if the invocand has exactly one row, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_row();</code></pre>

</dd>
<dt id="is_col()"><a id="is_col"></a>is_col()</dt>
<dd>

<p>Returns 1 if the invocand has exactly one column, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_col();</code></pre>

</dd>
<dt id="is_square()"><a id="is_square"></a>is_square()</dt>
<dd>

<p>Returns 1 is the invocand is square, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_square();</code></pre>

</dd>
<dt id="is_symmetric()"><a id="is_symmetric"></a>is_symmetric()</dt>
<dd>

<p>Returns 1 is the invocand is symmetric, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_symmetric();</code></pre>

<p>An symmetric matrix satisfies x(i,j) = x(j,i) for all i and j, for example</p>

<pre><code>    [  1  2 -3 ]
    [  2 -4  5 ]
    [ -3  5  6 ]</code></pre>

</dd>
<dt id="is_antisymmetric()"><a id="is_antisymmetric"></a>is_antisymmetric()</dt>
<dd>

<p>Returns 1 is the invocand is antisymmetric a.k.a. skew-symmetric, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_antisymmetric();</code></pre>

<p>An antisymmetric matrix satisfies x(i,j) = -x(j,i) for all i and j, for example</p>

<pre><code>    [  0  2 -3 ]
    [ -2  0  4 ]
    [  3 -4  0 ]</code></pre>

</dd>
<dt id="is_persymmetric()"><a id="is_persymmetric"></a>is_persymmetric()</dt>
<dd>

<p>Returns 1 is the invocand is persymmetric, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_persymmetric();</code></pre>

<p>A persymmetric matrix is a square matrix which is symmetric with respect to the anti-diagonal, e.g.:</p>

<pre><code>    [ f  h  j  k ]
    [ c  g  i  j ]
    [ b  d  g  h ]
    [ a  b  c  f ]</code></pre>

</dd>
<dt id="is_hankel()"><a id="is_hankel"></a>is_hankel()</dt>
<dd>

<p>Returns 1 is the invocand is a Hankel matric a.k.a. a catalecticant matrix, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_hankel();</code></pre>

<p>A Hankel matrix is a square matrix in which each ascending skew-diagonal from left to right is constant, e.g.:</p>

<pre><code>    [ e f g h i ]
    [ d e f g h ]
    [ c d e f g ]
    [ b c d e f ]
    [ a b c d e ]</code></pre>

</dd>
<dt id="is_zero()"><a id="is_zero"></a>is_zero()</dt>
<dd>

<p>Returns 1 is the invocand is a zero matrix, and 0 otherwise. A zero matrix contains no element whose value is different from zero.</p>

<pre><code>    $bool = $x -&gt; is_zero();</code></pre>

</dd>
<dt id="is_one()"><a id="is_one"></a>is_one()</dt>
<dd>

<p>Returns 1 is the invocand is a matrix of ones, and 0 otherwise. A matrix of ones contains no element whose value is different from one.</p>

<pre><code>    $bool = $x -&gt; is_one();</code></pre>

</dd>
<dt id="is_constant()"><a id="is_constant"></a>is_constant()</dt>
<dd>

<p>Returns 1 is the invocand is a constant matrix, and 0 otherwise. A constant matrix is a matrix where no two elements have different values.</p>

<pre><code>    $bool = $x -&gt; is_constant();</code></pre>

</dd>
<dt id="is_identity()"><a id="is_identity"></a>is_identity()</dt>
<dd>

<p>Returns 1 is the invocand is an identity matrix, and 0 otherwise. An identity matrix contains ones on the main diagonal and zeros elsewhere.</p>

<pre><code>    $bool = $x -&gt; is_identity();</code></pre>

</dd>
<dt id="is_exchg()"><a id="is_exchg"></a>is_exchg()</dt>
<dd>

<p>Returns 1 is the invocand is an exchange matrix, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_exchg();</code></pre>

<p>An exchange matrix contains ones on the main anti-diagonal and zeros elsewhere, for example</p>

<pre><code>    [ 0 0 1 ]
    [ 0 1 0 ]
    [ 1 0 0 ]</code></pre>

</dd>
<dt id="is_bool()"><a id="is_bool"></a>is_bool()</dt>
<dd>

<p>Returns 1 is the invocand is a boolean matrix, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_bool();</code></pre>

<p>A boolean matrix is a matrix is a matrix whose entries are either 0 or 1, for example</p>

<pre><code>    [ 0 1 1 ]
    [ 1 0 0 ]
    [ 0 1 0 ]</code></pre>

</dd>
<dt id="is_perm()"><a id="is_perm"></a>is_perm()</dt>
<dd>

<p>Returns 1 is the invocand is an permutation matrix, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_perm();</code></pre>

<p>A permutation matrix is a square matrix with exactly one 1 in each row and column, and all other elements 0, for example</p>

<pre><code>    [ 0 1 0 ]
    [ 1 0 0 ]
    [ 0 0 1 ]</code></pre>

</dd>
<dt id="is_int()"><a id="is_int"></a>is_int()</dt>
<dd>

<p>Returns 1 is the invocand is an integer matrix, i.e., a matrix of integers, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_int();</code></pre>

</dd>
<dt id="is_diag()"><a id="is_diag"></a>is_diag()</dt>
<dd>

<p>Returns 1 is the invocand is diagonal, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_diag();</code></pre>

<p>A diagonal matrix is a square matrix where all non-zero elements, if any, are on the main diagonal. It has the following pattern, where only the elements marked as <code>x</code> can be non-zero,</p>

<pre><code>    [ x 0 0 0 0 ]
    [ 0 x 0 0 0 ]
    [ 0 0 x 0 0 ]
    [ 0 0 0 x 0 ]
    [ 0 0 0 0 x ]</code></pre>

</dd>
<dt id="is_adiag()"><a id="is_adiag"></a>is_adiag()</dt>
<dd>

<p>Returns 1 is the invocand is anti-diagonal, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_adiag();</code></pre>

<p>A diagonal matrix is a square matrix where all non-zero elements, if any, are on the main antidiagonal. It has the following pattern, where only the elements marked as <code>x</code> can be non-zero,</p>

<pre><code>    [ 0 0 0 0 x ]
    [ 0 0 0 x 0 ]
    [ 0 0 x 0 0 ]
    [ 0 x 0 0 0 ]
    [ x 0 0 0 0 ]</code></pre>

</dd>
<dt id="is_tridiag()"><a id="is_tridiag"></a>is_tridiag()</dt>
<dd>

<p>Returns 1 is the invocand is tridiagonal, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_tridiag();</code></pre>

<p>A tridiagonal matrix is a square matrix with nonzero elements only on the diagonal and slots horizontally or vertically adjacent the diagonal (i.e., along the subdiagonal and superdiagonal). It has the following pattern, where only the elements marked as <code>x</code> can be non-zero,</p>

<pre><code>    [ x x 0 0 0 ]
    [ x x x 0 0 ]
    [ 0 x x x 0 ]
    [ 0 0 x x x ]
    [ 0 0 0 x x ]</code></pre>

</dd>
<dt id="is_atridiag()"><a id="is_atridiag"></a>is_atridiag()</dt>
<dd>

<p>Returns 1 is the invocand is anti-tridiagonal, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_tridiag();</code></pre>

<p>A anti-tridiagonal matrix is a square matrix with nonzero elements only on the anti-diagonal and slots horizontally or vertically adjacent the diagonal (i.e., along the anti-subdiagonal and anti-superdiagonal). It has the following pattern, where only the elements marked as <code>x</code> can be non-zero,</p>

<pre><code>    [ 0 0 0 x x ]
    [ 0 0 x x x ]
    [ 0 x x x 0 ]
    [ x x x 0 0 ]
    [ x x 0 0 0 ]</code></pre>

</dd>
<dt id="is_pentadiag()"><a id="is_pentadiag"></a>is_pentadiag()</dt>
<dd>

<p>Returns 1 is the invocand is pentadiagonal, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_pentadiag();</code></pre>

<p>A pentadiagonal matrix is a square matrix with nonzero elements only on the diagonal and the two diagonals above and below the main diagonal. It has the following pattern, where only the elements marked as <code>x</code> can be non-zero,</p>

<pre><code>    [ x x x 0 0 0 ]
    [ x x x x 0 0 ]
    [ x x x x x 0 ]
    [ 0 x x x x x ]
    [ 0 0 x x x x ]
    [ 0 0 0 x x x ]</code></pre>

</dd>
<dt id="is_apentadiag()"><a id="is_apentadiag"></a>is_apentadiag()</dt>
<dd>

<p>Returns 1 is the invocand is anti-pentadiagonal, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_pentadiag();</code></pre>

<p>A anti-pentadiagonal matrix is a square matrix with nonzero elements only on the anti-diagonal and two anti-diagonals above and below the main anti-diagonal. It has the following pattern, where only the elements marked as <code>x</code> can be non-zero,</p>

<pre><code>    [ 0 0 0 x x x ]
    [ 0 0 x x x x ]
    [ 0 x x x x x ]
    [ x x x x x 0 ]
    [ x x x x 0 0 ]
    [ x x x 0 0 0 ]</code></pre>

</dd>
<dt id="is_heptadiag()"><a id="is_heptadiag"></a>is_heptadiag()</dt>
<dd>

<p>Returns 1 is the invocand is heptadiagonal, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_heptadiag();</code></pre>

<p>A heptadiagonal matrix is a square matrix with nonzero elements only on the diagonal and the two diagonals above and below the main diagonal. It has the following pattern, where only the elements marked as <code>x</code> can be non-zero,</p>

<pre><code>    [ x x x x 0 0 ]
    [ x x x x x 0 ]
    [ x x x x x x ]
    [ x x x x x x ]
    [ 0 x x x x x ]
    [ 0 0 x x x x ]</code></pre>

</dd>
<dt id="is_aheptadiag()"><a id="is_aheptadiag"></a>is_aheptadiag()</dt>
<dd>

<p>Returns 1 is the invocand is anti-heptadiagonal, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_heptadiag();</code></pre>

<p>A anti-heptadiagonal matrix is a square matrix with nonzero elements only on the anti-diagonal and two anti-diagonals above and below the main anti-diagonal. It has the following pattern, where only the elements marked as <code>x</code> can be non-zero,</p>

<pre><code>    [ 0 0 x x x x ]
    [ 0 x x x x x ]
    [ x x x x x x ]
    [ x x x x x x ]
    [ x x x x x 0 ]
    [ x x x x 0 0 ]</code></pre>

</dd>
<dt id="is_band()"><a id="is_band"></a>is_band()</dt>
<dd>

<p>Returns 1 is the invocand is a band matrix with a specified bandwidth, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_band($k);</code></pre>

<p>A band matrix is a square matrix with nonzero elements only on the diagonal and on the <code>$k</code> diagonals above and below the main diagonal. The bandwidth <code>$k</code> must be non-negative.</p>

<pre><code>    $bool = $x -&gt; is_band(0);   # is $x diagonal?
    $bool = $x -&gt; is_band(1);   # is $x tridiagonal?
    $bool = $x -&gt; is_band(2);   # is $x pentadiagonal?
    $bool = $x -&gt; is_band(3);   # is $x heptadiagonal?</code></pre>

<p>See also <code><a href="./Math::Matrix.html#is_aband()">&quot;is_aband()&quot;</a></code> and <code><a href="./Math::Matrix.html#bandwidth()">&quot;bandwidth()&quot;</a></code>.</p>

</dd>
<dt id="is_aband()"><a id="is_aband"></a>is_aband()</dt>
<dd>

<p>Returns 1 is the invocand is &quot;anti-banded&quot; with a specified bandwidth, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_aband($k);</code></pre>

<p>Some examples</p>

<pre><code>    $bool = $x -&gt; is_aband(0);  # is $x anti-diagonal?
    $bool = $x -&gt; is_aband(1);  # is $x anti-tridiagonal?
    $bool = $x -&gt; is_aband(2);  # is $x anti-pentadiagonal?
    $bool = $x -&gt; is_aband(3);  # is $x anti-heptadiagonal?</code></pre>

<p>A band matrix is a square matrix with nonzero elements only on the diagonal and on the <code>$k</code> diagonals above and below the main diagonal. The bandwidth <code>$k</code> must be non-negative.</p>

<p>A &quot;anti-banded&quot; matrix is a square matrix with nonzero elements only on the anti-diagonal and <code>$k</code> anti-diagonals above and below the main anti-diagonal.</p>

<p>See also <code><a href="./Math::Matrix.html#is_band()">&quot;is_band()&quot;</a></code> and <code><a href="./Math::Matrix.html#bandwidth()">&quot;bandwidth()&quot;</a></code>.</p>

</dd>
<dt id="is_triu()"><a id="is_triu"></a>is_triu()</dt>
<dd>

<p>Returns 1 is the invocand is upper triangular, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_triu();</code></pre>

<p>An upper triangular matrix is a square matrix where all non-zero elements are on or above the main diagonal. It has the following pattern, where only the elements marked as <code>x</code> can be non-zero. It has the following pattern, where only the elements marked as <code>x</code> can be non-zero,</p>

<pre><code>    [ x x x x ]
    [ 0 x x x ]
    [ 0 0 x x ]
    [ 0 0 0 x ]</code></pre>

</dd>
<dt id="is_striu()"><a id="is_striu"></a>is_striu()</dt>
<dd>

<p>Returns 1 is the invocand is strictly upper triangular, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_striu();</code></pre>

<p>A strictly upper triangular matrix is a square matrix where all non-zero elements are strictly above the main diagonal. It has the following pattern, where only the elements marked as <code>x</code> can be non-zero,</p>

<pre><code>    [ 0 x x x ]
    [ 0 0 x x ]
    [ 0 0 0 x ]
    [ 0 0 0 0 ]</code></pre>

</dd>
<dt id="is_tril()"><a id="is_tril"></a>is_tril()</dt>
<dd>

<p>Returns 1 is the invocand is lower triangular, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_tril();</code></pre>

<p>A lower triangular matrix is a square matrix where all non-zero elements are on or below the main diagonal. It has the following pattern, where only the elements marked as <code>x</code> can be non-zero,</p>

<pre><code>    [ x 0 0 0 ]
    [ x x 0 0 ]
    [ x x x 0 ]
    [ x x x x ]</code></pre>

</dd>
<dt id="is_stril()"><a id="is_stril"></a>is_stril()</dt>
<dd>

<p>Returns 1 is the invocand is strictly lower triangular, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_stril();</code></pre>

<p>A strictly lower triangular matrix is a square matrix where all non-zero elements are strictly below the main diagonal. It has the following pattern, where only the elements marked as <code>x</code> can be non-zero,</p>

<pre><code>    [ 0 0 0 0 ]
    [ x 0 0 0 ]
    [ x x 0 0 ]
    [ x x x 0 ]</code></pre>

</dd>
<dt id="is_atriu()"><a id="is_atriu"></a>is_atriu()</dt>
<dd>

<p>Returns 1 is the invocand is upper anti-triangular, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_atriu();</code></pre>

<p>An upper anti-triangular matrix is a square matrix where all non-zero elements are on or above the main anti-diagonal. It has the following pattern, where only the elements marked as <code>x</code> can be non-zero,</p>

<pre><code>    [ x x x x ]
    [ x x x 0 ]
    [ x x 0 0 ]
    [ x 0 0 0 ]</code></pre>

</dd>
<dt id="is_satriu()"><a id="is_satriu"></a>is_satriu()</dt>
<dd>

<p>Returns 1 is the invocand is strictly upper anti-triangular, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_satriu();</code></pre>

<p>A strictly anti-triangular matrix is a square matrix where all non-zero elements are strictly above the main diagonal. It has the following pattern, where only the elements marked as <code>x</code> can be non-zero,</p>

<pre><code>    [ x x x 0 ]
    [ x x 0 0 ]
    [ x 0 0 0 ]
    [ 0 0 0 0 ]</code></pre>

</dd>
<dt id="is_atril()"><a id="is_atril"></a>is_atril()</dt>
<dd>

<p>Returns 1 is the invocand is lower anti-triangular, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_atril();</code></pre>

<p>A lower anti-triangular matrix is a square matrix where all non-zero elements are on or below the main anti-diagonal. It has the following pattern, where only the elements marked as <code>x</code> can be non-zero,</p>

<pre><code>    [ 0 0 0 x ]
    [ 0 0 x x ]
    [ 0 x x x ]
    [ x x x x ]</code></pre>

</dd>
<dt id="is_satril()"><a id="is_satril"></a>is_satril()</dt>
<dd>

<p>Returns 1 is the invocand is strictly lower anti-triangular, and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; is_satril();</code></pre>

<p>A strictly lower anti-triangular matrix is a square matrix where all non-zero elements are strictly below the main anti-diagonal. It has the following pattern, where only the elements marked as <code>x</code> can be non-zero,</p>

<pre><code>    [ 0 0 0 0 ]
    [ 0 0 0 x ]
    [ 0 0 x x ]
    [ 0 x x x ]</code></pre>

</dd>
</dl>

<h2 id="Identify-elements"><a id="Identify1"></a>Identify elements</h2>

<p>This section contains methods for identifying and locating elements within an array. See also <code><a href="./Math::Matrix.html#Scalar-comparison">&quot;Scalar comparison&quot;</a></code>.</p>

<dl>

<dt id="find()"><a id="find"></a>find()</dt>
<dd>

<p>Returns the location of each non-zero element.</p>

<pre><code>    $K = $x -&gt; find();          # linear index
    ($I, $J) = $x -&gt; find();    # subscripts</code></pre>

<p>For example, to find the linear index of each element that is greater than or equal to 1, use</p>

<pre><code>    $K = $x -&gt; sge(1) -&gt; find();</code></pre>

</dd>
<dt id="is_finite()"><a id="is_finite"></a>is_finite()</dt>
<dd>

<p>Returns a matrix of ones and zeros. The element is one if the corresponding element in the invocand matrix is finite, and zero otherwise.</p>

<pre><code>    $y = $x -&gt; is_finite();</code></pre>

</dd>
<dt id="is_inf()"><a id="is_inf"></a>is_inf()</dt>
<dd>

<p>Returns a matrix of ones and zeros. The element is one if the corresponding element in the invocand matrix is positive or negative infinity, and zero otherwise.</p>

<pre><code>    $y = $x -&gt; is_inf();</code></pre>

</dd>
<dt id="is_nan()"><a id="is_nan"></a>is_nan()</dt>
<dd>

<p>Returns a matrix of ones and zeros. The element is one if the corresponding element in the invocand matrix is a NaN (Not-a-Number), and zero otherwise.</p>

<pre><code>    $y = $x -&gt; is_nan();</code></pre>

</dd>
<dt id="all()"><a id="all"></a>all()</dt>
<dd>

<p>Tests whether all of the elements along various dimensions of a matrix are non-zero. If the dimension argument is not given, the first non-singleton dimension is used.</p>

<pre><code>    $y = $x -&gt; all($dim);
    $y = $x -&gt; all();</code></pre>

</dd>
<dt id="any()"><a id="any"></a>any()</dt>
<dd>

<p>Tests whether any of the elements along various dimensions of a matrix are non-zero. If the dimension argument is not given, the first non-singleton dimension is used.</p>

<pre><code>    $y = $x -&gt; any($dim);
    $y = $x -&gt; any();</code></pre>

</dd>
<dt id="cumall()"><a id="cumall"></a>cumall()</dt>
<dd>

<p>A cumulative variant of <code><a href="./Math::Matrix.html#all()">&quot;all()&quot;</a></code>. If the dimension argument is not given, the first non-singleton dimension is used.</p>

<pre><code>    $y = $x -&gt; cumall($dim);
    $y = $x -&gt; cumall();</code></pre>

</dd>
<dt id="cumany()"><a id="cumany"></a>cumany()</dt>
<dd>

<p>A cumulative variant of <code><a href="./Math::Matrix.html#all()">&quot;all()&quot;</a></code>. If the dimension argument is not given, the first non-singleton dimension is used.</p>

<pre><code>    $y = $x -&gt; cumany($dim);
    $y = $x -&gt; cumany();</code></pre>

</dd>
</dl>

<h2 id="Basic-properties"><a id="Basic"></a>Basic properties</h2>

<dl>

<dt id="size()"><a id="size"></a>size()</dt>
<dd>

<p>You can determine the dimensions of a matrix by calling:</p>

<pre><code>    ($m, $n) = $a -&gt; size;</code></pre>

</dd>
<dt id="nelm()"><a id="nelm"></a>nelm()</dt>
<dd>

<p>Returns the number of elements in the matrix.</p>

<pre><code>    $n = $x -&gt; nelm();</code></pre>

</dd>
<dt id="nrow()"><a id="nrow"></a>nrow()</dt>
<dd>

<p>Returns the number of rows.</p>

<pre><code>    $m = $x -&gt; nrow();</code></pre>

</dd>
<dt id="ncol()"><a id="ncol"></a>ncol()</dt>
<dd>

<p>Returns the number of columns.</p>

<pre><code>    $n = $x -&gt; ncol();</code></pre>

</dd>
<dt id="npag()"><a id="npag"></a>npag()</dt>
<dd>

<p>Returns the number of pages. A non-matrix has one page.</p>

<pre><code>    $n = $x -&gt; pag();</code></pre>

</dd>
<dt id="ndim()"><a id="ndim"></a>ndim()</dt>
<dd>

<p>Returns the number of dimensions. This is the number of dimensions along which the length is different from one.</p>

<pre><code>    $n = $x -&gt; ndim();</code></pre>

</dd>
<dt id="bandwidth()"><a id="bandwidth"></a>bandwidth()</dt>
<dd>

<p>Returns the bandwidth of a matrix. In scalar context, returns the number of the non-zero diagonal furthest away from the main diagonal. In list context, separate values are returned for the lower and upper bandwidth.</p>

<pre><code>    $n = $x -&gt; bandwidth();
    ($l, $u) = $x -&gt; bandwidth();</code></pre>

<p>The bandwidth is a non-negative integer. If the bandwidth is 0, the matrix is diagonal or zero. If the bandwidth is 1, the matrix is tridiagonal. If the bandwidth is 2, the matrix is pentadiagonal etc.</p>

<p>A matrix with the following pattern, where <code>x</code> denotes a non-zero value, would return 2 in scalar context, and (1,2) in list context.</p>

<pre><code>    [ x x x 0 0 0 ]
    [ x x x x 0 0 ]
    [ 0 x x x x 0 ]
    [ 0 0 x x x x ]
    [ 0 0 0 x x x ]
    [ 0 0 0 0 x x ]</code></pre>

<p>See also <code><a href="./Math::Matrix.html#is_band()">&quot;is_band()&quot;</a></code> and <code><a href="./Math::Matrix.html#is_aband()">&quot;is_aband()&quot;</a></code>.</p>

</dd>
</dl>

<h2 id="Manipulate-matrices"><a id="Manipulate"></a>Manipulate matrices</h2>

<p>These methods are for combining matrices, splitting matrices, extracing parts of a matrix, inserting new parts into a matrix, deleting parts of a matrix etc. There are also methods for shuffling elements around (relocating elements) inside a matrix.</p>

<p>These methods are not concerned with the values of the elements.</p>

<dl>

<dt id="catrow()"><a id="catrow"></a>catrow()</dt>
<dd>

<p>Concatenate rows, i.e., concatenate matrices vertically. Any number of arguments is allowed. All non-empty matrices must have the same number or columns. The result is a new matrix.</p>

<pre><code>    $x = Math::Matrix -&gt; new([1, 2], [4, 5]);   # 2-by-2 matrix
    $y = Math::Matrix -&gt; new([3, 6]);           # 1-by-2 matrix
    $z = $x -&gt; catrow($y);                      # 3-by-2 matrix</code></pre>

</dd>
<dt id="catcol()"><a id="catcol"></a>catcol()</dt>
<dd>

<p>Concatenate columns, i.e., matrices horizontally. Any number of arguments is allowed. All non-empty matrices must have the same number or rows. The result is a new matrix.</p>

<pre><code>    $x = Math::Matrix -&gt; new([1, 2], [4, 5]);   # 2-by-2 matrix
    $y = Math::Matrix -&gt; new([3], [6]);         # 2-by-1 matrix
    $z = $x -&gt; catcol($y);                      # 2-by-3 matrix</code></pre>

</dd>
<dt id="getrow()"><a id="getrow"></a>getrow()</dt>
<dd>

<p>Get the specified row(s). Returns a new matrix with the specified rows. The number of rows in the output is identical to the number of elements in the input.</p>

<pre><code>    $y = $x -&gt; getrow($i);                  # get one
    $y = $x -&gt; getrow([$i0, $i1, $i2]);     # get multiple</code></pre>

</dd>
<dt id="getcol()"><a id="getcol"></a>getcol()</dt>
<dd>

<p>Get the specified column(s). Returns a new matrix with the specified columns. The number of columns in the output is identical to the number of elements in the input.</p>

<pre><code>    $y = $x -&gt; getcol($j);                  # get one
    $y = $x -&gt; getcol([$j0, $j1, $j2]);     # get multiple</code></pre>

</dd>
<dt id="delrow()"><a id="delrow"></a>delrow()</dt>
<dd>

<p>Delete row(s). Returns a new matrix identical to the invocand but with the specified row(s) deleted.</p>

<pre><code>    $y = $x -&gt; delrow($i);                  # delete one
    $y = $x -&gt; delrow([$i0, $i1, $i2]);     # delete multiple</code></pre>

</dd>
<dt id="delcol()"><a id="delcol"></a>delcol()</dt>
<dd>

<p>Delete column(s). Returns a new matrix identical to the invocand but with the specified column(s) deleted.</p>

<pre><code>    $y = $x -&gt; delcol($j);                  # delete one
    $y = $x -&gt; delcol([$j0, $j1, $j2]);     # delete multiple</code></pre>

</dd>
<dt id="concat()"><a id="concat"></a>concat()</dt>
<dd>

<p>Concatenate two matrices horizontally. The matrices must have the same number of rows. The result is a new matrix or <b>undef</b> in case of error.</p>

<pre><code>    $x = Math::Matrix -&gt; new([1, 2], [4, 5]);   # 2-by-2 matrix
    $y = Math::Matrix -&gt; new([3], [6]);         # 2-by-1 matrix
    $z = $x -&gt; concat($y);                      # 2-by-3 matrix</code></pre>

</dd>
<dt id="splicerow()"><a id="splicerow"></a>splicerow()</dt>
<dd>

<p>Row splicing. This is like Perl&#39;s built-in splice() function, except that it works on the rows of a matrix.</p>

<pre><code>    $y = $x -&gt; splicerow($offset);
    $y = $x -&gt; splicerow($offset, $length);
    $y = $x -&gt; splicerow($offset, $length, $a, $b, ...);</code></pre>

<p>The built-in splice() function modifies the first argument and returns the removed elements, if any. However, since splicerow() does not modify the invocand, it returns the modified version as the first output argument and the removed part as a (possibly empty) second output argument.</p>

<pre><code>    $x = Math::Matrix -&gt; new([[ 1,  2],
                              [ 3,  4],
                              [ 5,  6],
                              [ 7,  8]]);
    $a = Math::Matrix -&gt; new([[11, 12],
                              [13, 14]]);
    ($y, $z) = $x -&gt; splicerow(1, 2, $a);</code></pre>

<p>Gives <code>$y</code></p>

<pre><code>    [  1  2 ]
    [ 11 12 ]
    [ 13 14 ]
    [  7  8 ]</code></pre>

<p>and <code>$z</code></p>

<pre><code>    [  3  4 ]
    [  5  6 ]</code></pre>

</dd>
<dt id="splicecol()"><a id="splicecol"></a>splicecol()</dt>
<dd>

<p>Column splicing. This is like Perl&#39;s built-in splice() function, except that it works on the columns of a matrix.</p>

<pre><code>    $y = $x -&gt; splicecol($offset);
    $y = $x -&gt; splicecol($offset, $length);
    $y = $x -&gt; splicecol($offset, $length, $a, $b, ...);</code></pre>

<p>The built-in splice() function modifies the first argument and returns the removed elements, if any. However, since splicecol() does not modify the invocand, it returns the modified version as the first output argument and the removed part as a (possibly empty) second output argument.</p>

<pre><code>    $x = Math::Matrix -&gt; new([[ 1, 3, 5, 7 ],
                              [ 2, 4, 6, 8 ]]);
    $a = Math::Matrix -&gt; new([[11, 13],
                              [12, 14]]);
    ($y, $z) = $x -&gt; splicerow(1, 2, $a);</code></pre>

<p>Gives <code>$y</code></p>

<pre><code>    [ 1  11  13  7 ]
    [ 2  12  14  8 ]</code></pre>

<p>and <code>$z</code></p>

<pre><code>    [ 3  5 ]
    [ 4  6 ]</code></pre>

</dd>
<dt id="swaprc()"><a id="swaprc"></a>swaprc()</dt>
<dd>

<p>Swap rows and columns. This method does nothing but shuffle elements around. For real numbers, swaprc() is identical to the transpose() method.</p>

<p>A subclass implementing a matrix of complex numbers should provide a transpose() method that also takes the complex conjugate of each elements. The swaprc() method, on the other hand, should only shuffle elements around.</p>

</dd>
<dt id="flipud()"><a id="flipud"></a>flipud()</dt>
<dd>

<p>Flip upside-down, i.e., flip along dimension 1.</p>

<pre><code>    $y = $x -&gt; flipud();</code></pre>

</dd>
<dt id="fliplr()"><a id="fliplr"></a>fliplr()</dt>
<dd>

<p>Flip left-to-right, i.e., flip along dimension 2.</p>

<pre><code>    $y = $x -&gt; fliplr();</code></pre>

</dd>
<dt id="flip()"><a id="flip"></a>flip()</dt>
<dd>

<p>Flip along various dimensions of a matrix. If the dimension argument is not given, the first non-singleton dimension is used.</p>

<pre><code>    $y = $x -&gt; flip($dim);
    $y = $x -&gt; flip();</code></pre>

<p>See also <code><a href="./Math::Matrix.html#flipud()">&quot;flipud()&quot;</a></code> and <code><a href="./Math::Matrix.html#fliplr()">&quot;fliplr()&quot;</a></code>.</p>

</dd>
<dt id="rot90()"><a id="rot90"></a>rot90()</dt>
<dd>

<p>Rotate 90 degrees counterclockwise.</p>

<pre><code>    $y = $x -&gt; rot90();     # rotate 90 degrees counterclockwise
    $y = $x -&gt; rot90($n);   # rotate 90*$n degrees counterclockwise</code></pre>

</dd>
<dt id="rot180()"><a id="rot180"></a>rot180()</dt>
<dd>

<p>Rotate 180 degrees.</p>

<pre><code>    $y = $x -&gt; rot180();</code></pre>

</dd>
<dt id="rot270()"><a id="rot270"></a>rot270()</dt>
<dd>

<p>Rotate 270 degrees counterclockwise, i.e., 90 degrees clockwise.</p>

<pre><code>    $y = $x -&gt; rot270();</code></pre>

</dd>
<dt id="repelm()"><a id="repelm"></a>repelm()</dt>
<dd>

<p>Repeat elements.</p>

<pre><code>    $x -&gt; repelm($y);</code></pre>

<p>Repeats each element in $x the number of times specified in $y.</p>

<p>If $x is the matrix</p>

<pre><code>    [ 4 5 6 ]
    [ 7 8 9 ]</code></pre>

<p>and $y is</p>

<pre><code>    [ 3 2 ]</code></pre>

<p>the returned matrix is</p>

<pre><code>    [ 4 4 5 5 6 6 ]
    [ 4 4 5 5 6 6 ]
    [ 4 4 5 5 6 6 ]
    [ 7 7 8 8 9 9 ]
    [ 7 7 8 8 9 9 ]
    [ 7 7 8 8 9 9 ]</code></pre>

</dd>
<dt id="repmat()"><a id="repmat"></a>repmat()</dt>
<dd>

<p>Repeat elements.</p>

<pre><code>    $x -&gt; repmat($y);</code></pre>

<p>Repeats the matrix $x the number of times specified in $y.</p>

<p>If $x is the matrix</p>

<pre><code>    [ 4 5 6 ]
    [ 7 8 9 ]</code></pre>

<p>and $y is</p>

<pre><code>    [ 3 2 ]</code></pre>

<p>the returned matrix is</p>

<pre><code>    [ 4 5 6 4 5 6 ]
    [ 7 8 9 7 8 9 ]
    [ 4 5 6 4 5 6 ]
    [ 7 8 9 7 8 9 ]
    [ 4 5 6 4 5 6 ]
    [ 7 8 9 7 8 9 ]</code></pre>

</dd>
<dt id="reshape()"><a id="reshape"></a>reshape()</dt>
<dd>

<p>Returns a reshaped copy of a matrix. The reshaping is done by creating a new matrix and looping over the elements in column major order. The new matrix must have the same number of elements as the invocand matrix. The following returns an <code>$m</code>-by-<code>$n</code> matrix,</p>

<pre><code>    $y = $x -&gt; reshape($m, $n);</code></pre>

<p>The code</p>

<pre><code>    $x = Math::Matrix -&gt; new([[1, 3, 5, 7], [2, 4, 6, 8]]);
    $y = $x -&gt; reshape(4, 2);</code></pre>

<p>creates the matrix $x</p>

<pre><code>    [ 1  3  5  7 ]
    [ 2  4  6  8 ]</code></pre>

<p>and returns a reshaped copy $y</p>

<pre><code>    [ 1  5 ]
    [ 2  6 ]
    [ 3  7 ]
    [ 4  8 ]</code></pre>

</dd>
<dt id="to_row()"><a id="to_row"></a>to_row()</dt>
<dd>

<p>Reshape to a row.</p>

<pre><code>    $x -&gt; to_row();</code></pre>

<p>This method reshapes the matrix into a single row. It is essentially the same as, but faster than,</p>

<pre><code>    $x -&gt; reshape(1, $x -&gt; nelm());</code></pre>

</dd>
<dt id="to_col()"><a id="to_col"></a>to_col()</dt>
<dd>

<p>Reshape to a column.</p>

<pre><code>    $y = $x -&gt; to_col();</code></pre>

<p>This method reshapes the matrix into a single column. It is essentially the same as, but faster than,</p>

<pre><code>    $x -&gt; reshape($x -&gt; nelm(), 1);</code></pre>

</dd>
<dt id="to_permmat()"><a id="to_permmat"></a>to_permmat()</dt>
<dd>

<p>Permutation vector to permutation matrix. Converts a vector of zero-based permutation indices to a permutation matrix.</p>

<pre><code>    $P = $v -&gt; to_permmat();</code></pre>

<p>For example</p>

<pre><code>    $v = Math::Matrix -&gt; new([[0, 3, 1, 4, 2]]);
    $m = $v -&gt; to_permmat();</code></pre>

<p>gives the permutation matrix <code>$m</code></p>

<pre><code>    [ 1 0 0 0 0 ]
    [ 0 0 0 1 0 ]
    [ 0 1 0 0 0 ]
    [ 0 0 0 0 1 ]
    [ 0 0 1 0 0 ]</code></pre>

</dd>
<dt id="to_permvec()"><a id="to_permvec"></a>to_permvec()</dt>
<dd>

<p>Permutation matrix to permutation vector. Converts a permutation matrix to a vector of zero-based permutation indices.</p>

<pre><code>    $v = $P -&gt; to_permvec();

    $v = Math::Matrix -&gt; new([[0, 3, 1, 4, 2]]);
    $m = $v -&gt; to_permmat();</code></pre>

<p>Gives the permutation matrix <code>$m</code></p>

<pre><code>    [ 1 0 0 0 0 ]
    [ 0 0 0 1 0 ]
    [ 0 1 0 0 0 ]
    [ 0 0 0 0 1 ]
    [ 0 0 1 0 0 ]</code></pre>

<p>See also <code><a href="./Math::Matrix.html#to_permmat()">&quot;to_permmat()&quot;</a></code>.</p>

</dd>
<dt id="triu()"><a id="triu"></a>triu()</dt>
<dd>

<p>Upper triangular part. Extract the upper triangular part of a matrix and set all other elements to zero.</p>

<pre><code>    $y = $x -&gt; triu();
    $y = $x -&gt; triu($n);</code></pre>

<p>The optional second argument specifies how many diagonals above or below the main diagonal should also be set to zero. The default value of <code>$n</code> is zero which includes the main diagonal.</p>

</dd>
<dt id="tril()"><a id="tril"></a>tril()</dt>
<dd>

<p>Lower triangular part. Extract the lower triangular part of a matrix and set all other elements to zero.</p>

<pre><code>    $y = $x -&gt; tril();
    $y = $x -&gt; tril($n);</code></pre>

<p>The optional second argument specifies how many diagonals above or below the main diagonal should also be set to zero. The default value of <code>$n</code> is zero which includes the main diagonal.</p>

</dd>
<dt id="slice()"><a id="slice"></a>slice()</dt>
<dd>

<p>Extract columns:</p>

<pre><code>    a-&gt;slice(1,3,5);</code></pre>

</dd>
<dt id="diagonal_vector()"><a id="diagonal_vector"></a>diagonal_vector()</dt>
<dd>

<p>Extract the diagonal as an array:</p>

<pre><code>    $diag = $a-&gt;diagonal_vector;</code></pre>

</dd>
<dt id="tridiagonal_vector()"><a id="tridiagonal_vector"></a>tridiagonal_vector()</dt>
<dd>

<p>Extract the diagonals that make up a tridiagonal matrix:</p>

<pre><code>    ($main_d, $upper_d, $lower_d) = $a-&gt;tridiagonal_vector;</code></pre>

</dd>
</dl>

<h2 id="Mathematical-functions"><a id="Mathematical"></a>Mathematical functions</h2>

<h3 id="Addition">Addition</h3>

<dl>

<dt id="add()"><a id="add"></a>add()</dt>
<dd>

<p>Addition. If one operands is a scalar, it is treated like a constant matrix with the same size as the other operand. Otherwise ordinary matrix addition is performed.</p>

<pre><code>    $z = $x -&gt; add($y);</code></pre>

<p>See also <code><a href="./Math::Matrix.html#madd()">&quot;madd()&quot;</a></code> and <code><a href="./Math::Matrix.html#sadd()">&quot;sadd()&quot;</a></code>.</p>

</dd>
<dt id="madd()"><a id="madd"></a>madd()</dt>
<dd>

<p>Matrix addition. Add two matrices of the same dimensions. An error is thrown if the matrices don&#39;t have the same size.</p>

<pre><code>    $z = $x -&gt; madd($y);</code></pre>

<p>See also <code><a href="./Math::Matrix.html#add()">&quot;add()&quot;</a></code> and <code><a href="./Math::Matrix.html#sadd()">&quot;sadd()&quot;</a></code>.</p>

</dd>
<dt id="sadd()"><a id="sadd"></a>sadd()</dt>
<dd>

<p>Scalar (element by element) addition with scalar expansion. This method places no requirements on the size of the input matrices.</p>

<pre><code>    $z = $x -&gt; sadd($y);</code></pre>

<p>See also <code><a href="./Math::Matrix.html#add()">&quot;add()&quot;</a></code> and <code><a href="./Math::Matrix.html#madd()">&quot;madd()&quot;</a></code>.</p>

</dd>
</dl>

<h3 id="Subtraction">Subtraction</h3>

<dl>

<dt id="sub()"><a id="sub"></a>sub()</dt>
<dd>

<p>Subtraction. If one operands is a scalar, it is treated as a constant matrix with the same size as the other operand. Otherwise, ordinarly matrix subtraction is performed.</p>

<pre><code>    $z = $x -&gt; sub($y);</code></pre>

<p>See also <code><a href="./Math::Matrix.html#msub()">&quot;msub()&quot;</a></code> and <code><a href="./Math::Matrix.html#ssub()">&quot;ssub()&quot;</a></code>.</p>

</dd>
<dt id="msub()"><a id="msub"></a>msub()</dt>
<dd>

<p>Matrix subtraction. Subtract two matrices of the same size. An error is thrown if the matrices don&#39;t have the same size.</p>

<pre><code>    $z = $x -&gt; msub($y);</code></pre>

<p>See also <code><a href="./Math::Matrix.html#sub()">&quot;sub()&quot;</a></code> and <code><a href="./Math::Matrix.html#ssub()">&quot;ssub()&quot;</a></code>.</p>

</dd>
<dt id="ssub()"><a id="ssub"></a>ssub()</dt>
<dd>

<p>Scalar (element by element) subtraction with scalar expansion. This method places no requirements on the size of the input matrices.</p>

<pre><code>    $z = $x -&gt; ssub($y);</code></pre>

<p>See also <code><a href="./Math::Matrix.html#sub()">&quot;sub()&quot;</a></code> and <code><a href="./Math::Matrix.html#msub()">&quot;msub()&quot;</a></code>.</p>

</dd>
<dt id="subtract()"><a id="subtract"></a>subtract()</dt>
<dd>

<p>This is an alias for <code><a href="./Math::Matrix.html#msub()">&quot;msub()&quot;</a></code>.</p>

</dd>
</dl>

<h3 id="Negation">Negation</h3>

<dl>

<dt id="neg()"><a id="neg"></a>neg()</dt>
<dd>

<p>Negation. Negate a matrix.</p>

<pre><code>    $y = $x -&gt; neg();</code></pre>

<p>It is effectively equivalent to</p>

<pre><code>    $y = $x -&gt; map(sub { -$_ });</code></pre>

</dd>
<dt id="negative()"><a id="negative"></a>negative()</dt>
<dd>

<p>This is an alias for <code><a href="./Math::Matrix.html#neg()">&quot;neg()&quot;</a></code>.</p>

</dd>
</dl>

<h3 id="Multiplication">Multiplication</h3>

<dl>

<dt id="mul()"><a id="mul"></a>mul()</dt>
<dd>

<p>Multiplication. If one operands is a scalar, it is treated as a constant matrix with the same size as the other operand. Otherwise, ordinary matrix multiplication is performed.</p>

<pre><code>    $z = $x -&gt; mul($y);</code></pre>

</dd>
<dt id="mmul()"><a id="mmul"></a>mmul()</dt>
<dd>

<p>Matrix multiplication. An error is thrown if the sizes don&#39;t match; the number of columns in the first operand must be equal to the number of rows in the second operand.</p>

<pre><code>    $z = $x -&gt; mmul($y);</code></pre>

</dd>
<dt id="smul()"><a id="smul"></a>smul()</dt>
<dd>

<p>Scalar (element by element) multiplication with scalar expansion. This method places no requirements on the size of the input matrices.</p>

<pre><code>    $z = $x -&gt; smul($y);</code></pre>

</dd>
<dt id="mmuladd()"><a id="mmuladd"></a>mmuladd()</dt>
<dd>

<p>Matrix fused multiply and add. If <code>$x</code> is a <code>$p</code>-by-<code>$q</code> matrix, then <code>$y</code> must be a <code>$q</code>-by-<code>$r</code> matrix and <code>$z</code> must be a <code>$p</code>-by-<code>$r</code> matrix. An error is thrown if the sizes don&#39;t match.</p>

<pre><code>    $w = $x -&gt; mmuladd($y, $z);</code></pre>

<p>The fused multiply and add is equivalent to, but computed with higher accuracy than</p>

<pre><code>    $w = $x -&gt; mmul($y) -&gt; madd($z);</code></pre>

<p>This method can be used to improve the solution of linear systems.</p>

</dd>
<dt id="kron()"><a id="kron"></a>kron()</dt>
<dd>

<p>Kronecker tensor product.</p>

<pre><code>    $A -&gt; kronprod($B);</code></pre>

<p>If <code>$A</code> is an <code>$m</code>-by-<code>$n</code> matrix and <code>$B</code> is a <code>$p</code>-by-<code>$q</code> matrix, then <code>$A -&gt; kron($B)</code> is an <code>$m</code>*<code>$p</code>-by-<code>$n</code>*<code>$q</code> matrix formed by taking all possible products between the elements of <code>$A</code> and the elements of <code>$B</code>.</p>

</dd>
<dt id="multiply()"><a id="multiply"></a>multiply()</dt>
<dd>

<p>This is an alias for <code><a href="./Math::Matrix.html#mmul()">&quot;mmul()&quot;</a></code>.</p>

</dd>
<dt id="multiply_scalar()"><a id="multiply_scalar"></a>multiply_scalar()</dt>
<dd>

<p>Multiplies a matrix and a scalar resulting in a matrix of the same dimensions with each element scaled with the scalar.</p>

<pre><code>    $a-&gt;multiply_scalar(2);  scale matrix by factor 2</code></pre>

</dd>
</dl>

<h3 id="Powers">Powers</h3>

<dl>

<dt id="pow()"><a id="pow"></a>pow()</dt>
<dd>

<p>Power function.</p>

<p>This is an alias for <code><a href="./Math::Matrix.html#mpow()">&quot;mpow()&quot;</a></code>.</p>

<p>See also <code><a href="./Math::Matrix.html#spow()">&quot;spow()&quot;</a></code>.</p>

</dd>
<dt id="mpow()"><a id="mpow"></a>mpow()</dt>
<dd>

<p>Matrix power. The second operand must be a non-negative integer.</p>

<pre><code>    $y = $x -&gt; mpow($n);</code></pre>

<p>The following example</p>

<pre><code>    $x = Math::Matrix -&gt; new([[0, -2],[1, 4]]);
    $y = 4;
    $z = $x -&gt; pow($y);</code></pre>

<p>returns the matrix</p>

<pre><code>    [ -28  -96 ]
    [  48  164 ]</code></pre>

<p>See also <code><a href="./Math::Matrix.html#spow()">&quot;spow()&quot;</a></code>.</p>

</dd>
<dt id="spow()"><a id="spow"></a>spow()</dt>
<dd>

<p>Scalar (element by element) power function. This method doesn&#39;t require the matrices to have the same size.</p>

<pre><code>    $z = $x -&gt; spow($y);</code></pre>

<p>See also <code><a href="./Math::Matrix.html#mpow()">&quot;mpow()&quot;</a></code>.</p>

</dd>
</dl>

<h3 id="Inversion">Inversion</h3>

<dl>

<dt id="inv()"><a id="inv"></a>inv()</dt>
<dd>

<p>This is an alias for <code><a href="./Math::Matrix.html#minv()">&quot;minv()&quot;</a></code>.</p>

</dd>
<dt id="invert()"><a id="invert"></a>invert()</dt>
<dd>

<p>Invert a Matrix using <code>solve</code>.</p>

</dd>
<dt id="minv()"><a id="minv"></a>minv()</dt>
<dd>

<p>Matrix inverse. Invert a matrix.</p>

<pre><code>    $y = $x -&gt; inv();</code></pre>

<p>See the section <a href="./Math::Matrix.html#IMPROVING-THE-SOLUTION-OF-LINEAR-SYSTEMS">&quot;IMPROVING THE SOLUTION OF LINEAR SYSTEMS&quot;</a> for a list of additional parameters that can be used for trying to obtain a better solution through iteration.</p>

</dd>
<dt id="sinv()"><a id="sinv"></a>sinv()</dt>
<dd>

<p>Scalar (element by element) inverse. Invert each element in a matrix.</p>

<pre><code>    $y = $x -&gt; sinv();</code></pre>

</dd>
<dt id="mldiv()"><a id="mldiv"></a>mldiv()</dt>
<dd>

<p>Matrix left division. Returns the solution x of the linear system of equations A*x = y, by computing A^(-1)*y.</p>

<pre><code>    $x = $y -&gt; mldiv($A);</code></pre>

<p>This method also handles overdetermined and underdetermined systems. There are three cases</p>

<ul>

<li><p>If A is a square matrix, then</p>

<pre><code>    x = A\y = inv(A)*y</code></pre>

<p>so that A*x = y to within round-off accuracy.</p>

</li>
<li><p>If A is an M-by-N matrix where M &gt; N, then A\y is computed as</p>

<pre><code>    A\y = (A&#39;*A)\(A&#39;*y) = inv(A&#39;*A)*(A&#39;*y)</code></pre>

<p>where A&#39; denotes the transpose of A. The returned matrix is the least squares solution to the linear system of equations A*x = y, if it exists. The matrix A&#39;*A must be non-singular.</p>

</li>
<li><p>If A is an where M &lt; N, then A\y is computed as</p>

<pre><code>    A\y = A&#39;*((A*A&#39;)\y)</code></pre>

<p>This solution is not unique. The matrix A*A&#39; must be non-singular.</p>

</li>
</ul>

<p>See the section <a href="./Math::Matrix.html#IMPROVING-THE-SOLUTION-OF-LINEAR-SYSTEMS">&quot;IMPROVING THE SOLUTION OF LINEAR SYSTEMS&quot;</a> for a list of additional parameters that can be used for trying to obtain a better solution through iteration.</p>

</dd>
<dt id="sldiv()"><a id="sldiv"></a>sldiv()</dt>
<dd>

<p>Scalar (left) division.</p>

<pre><code>    $x -&gt; sldiv($y);</code></pre>

<p>For scalars, there is no difference between left and right division, so this is just an alias for <code><a href="./Math::Matrix.html#sdiv()">&quot;sdiv()&quot;</a></code>.</p>

</dd>
<dt id="mrdiv()"><a id="mrdiv"></a>mrdiv()</dt>
<dd>

<p>Matrix right division. Returns the solution x of the linear system of equations x*A = y, by computing x = y/A = y*inv(A) = (A&#39;\y&#39;)&#39;, where A&#39; and y&#39; denote the transpose of A and y, respectively, and \ is matrix left division (see <code><a href="./Math::Matrix.html#mldiv()">&quot;mldiv()&quot;</a></code>).</p>

<pre><code>    $x = $y -&gt; mrdiv($A);</code></pre>

<p>See the section <a href="./Math::Matrix.html#IMPROVING-THE-SOLUTION-OF-LINEAR-SYSTEMS">&quot;IMPROVING THE SOLUTION OF LINEAR SYSTEMS&quot;</a> for a list of additional parameters that can be used for trying to obtain a better solution through iteration.</p>

</dd>
<dt id="srdiv()"><a id="srdiv"></a>srdiv()</dt>
<dd>

<p>Scalar (right) division.</p>

<pre><code>    $x -&gt; srdiv($y);</code></pre>

<p>For scalars, there is no difference between left and right division, so this is just an alias for <code><a href="./Math::Matrix.html#sdiv()">&quot;sdiv()&quot;</a></code>.</p>

</dd>
<dt id="sdiv()"><a id="sdiv"></a>sdiv()</dt>
<dd>

<p>Scalar division. Performs scalar (element by element) division.</p>

<pre><code>    $x -&gt; sdiv($y);</code></pre>

</dd>
<dt id="mpinv()"><a id="mpinv"></a>mpinv()</dt>
<dd>

<p>Matrix pseudo-inverse, <code>(A&#39;*A)^(-1)*A&#39;</code>, where &quot;<code>&#39;</code>&quot; is the transpose operator.</p>

<p>See the section <a href="./Math::Matrix.html#IMPROVING-THE-SOLUTION-OF-LINEAR-SYSTEMS">&quot;IMPROVING THE SOLUTION OF LINEAR SYSTEMS&quot;</a> for a list of additional parameters that can be used for trying to obtain a better solution through iteration.</p>

</dd>
<dt id="pinv()"><a id="pinv"></a>pinv()</dt>
<dd>

<p>This is an alias for <code><a href="./Math::Matrix.html#mpinv()">&quot;mpinv()&quot;</a></code>.</p>

</dd>
<dt id="pinvert()"><a id="pinvert"></a>pinvert()</dt>
<dd>

<p>This is an alias for <code><a href="./Math::Matrix.html#mpinv()">&quot;mpinv()&quot;</a></code>.</p>

</dd>
<dt id="solve()"><a id="solve"></a>solve()</dt>
<dd>

<p>Solves a equation system given by the matrix. The number of colums must be greater than the number of rows. If variables are dependent from each other, the second and all further of the dependent coefficients are 0. This means the method can handle such systems. The method returns a matrix containing the solutions in its columns or <b>undef</b> in case of error.</p>

</dd>
</dl>

<h3 id="Factorisation">Factorisation</h3>

<dl>

<dt id="chol()"><a id="chol"></a>chol()</dt>
<dd>

<p>Cholesky decomposition.</p>

<pre><code>    $L = $A -&gt; chol();</code></pre>

<p>Every symmetric, positive definite matrix A can be decomposed into a product of a unique lower triangular matrix L and its transpose, so that A = L*L&#39;, where L&#39; denotes the transpose of L. L is called the Cholesky factor of A.</p>

</dd>
</dl>

<h3 id="Miscellaneous-matrix-functions"><a id="Miscellaneous"></a>Miscellaneous matrix functions</h3>

<dl>

<dt id="transpose()"><a id="transpose"></a>transpose()</dt>
<dd>

<p>Returns the transposed matrix. This is the matrix where colums and rows of the argument matrix are swapped.</p>

<p>A subclass implementing matrices of complex numbers should provide a <code><a href="./Math::Matrix.html#transpose()">&quot;transpose()&quot;</a></code> method that takes the complex conjugate of each element.</p>

</dd>
<dt id="minormatrix()"><a id="minormatrix"></a>minormatrix()</dt>
<dd>

<p>Minor matrix. The (i,j) minor matrix of a matrix is identical to the original matrix except that row i and column j has been removed.</p>

<pre><code>    $y = $x -&gt; minormatrix($i, $j);</code></pre>

<p>See also <code><a href="./Math::Matrix.html#minor()">&quot;minor()&quot;</a></code>.</p>

</dd>
<dt id="minor()"><a id="minor"></a>minor()</dt>
<dd>

<p>Minor. The (i,j) minor of a matrix is the determinant of the (i,j) minor matrix.</p>

<pre><code>    $y = $x -&gt; minor($i, $j);</code></pre>

<p>See also <code><a href="./Math::Matrix.html#minormatrix()">&quot;minormatrix()&quot;</a></code>.</p>

</dd>
<dt id="cofactormatrix()"><a id="cofactormatrix"></a>cofactormatrix()</dt>
<dd>

<p>Cofactor matrix. Element (i,j) in the cofactor matrix is the (i,j) cofactor, which is (-1)^(i+j) multiplied by the determinant of the (i,j) minor matrix.</p>

<pre><code>    $y = $x -&gt; cofactormatrix();</code></pre>

</dd>
<dt id="cofactor()"><a id="cofactor"></a>cofactor()</dt>
<dd>

<p>Cofactor. The (i,j) cofactor of a matrix is (-1)**(i+j) times the (i,j) minor of the matrix.</p>

<pre><code>    $y = $x -&gt; cofactor($i, $j);</code></pre>

</dd>
<dt id="adjugate()"><a id="adjugate"></a>adjugate()</dt>
<dd>

<p>Adjugate of a matrix. The adjugate, also called classical adjoint or adjunct, of a square matrix is the transpose of the cofactor matrix.</p>

<pre><code>    $y = $x -&gt; adjugate();</code></pre>

</dd>
<dt id="det()"><a id="det"></a>det()</dt>
<dd>

<p>Determinant. Returns the determinant of a matrix. The matrix must be square.</p>

<pre><code>    $y = $x -&gt; det();</code></pre>

<p>The matrix is computed by forward elimination, which might cause round-off errors. So for example, the determinant might be a non-integer even for an integer matrix.</p>

</dd>
<dt id="determinant()"><a id="determinant"></a>determinant()</dt>
<dd>

<p>This is an alias for <code><a href="./Math::Matrix.html#det()">&quot;det()&quot;</a></code>.</p>

</dd>
<dt id="detr()"><a id="detr"></a>detr()</dt>
<dd>

<p>Determinant. Returns the determinant of a matrix. The matrix must be square.</p>

<pre><code>    $y = $x -&gt; determinant();</code></pre>

<p>The determinant is computed by recursion, so it is generally much slower than <code><a href="./Math::Matrix.html#det()">&quot;det()&quot;</a></code>.</p>

</dd>
</dl>

<h3 id="Elementwise-mathematical-functions"><a id="Elementwise"></a>Elementwise mathematical functions</h3>

<p>These method work on each element of a matrix.</p>

<dl>

<dt id="int()"><a id="int"></a>int()</dt>
<dd>

<p>Truncate to integer. Truncates each element to an integer.</p>

<pre><code>    $y = $x -&gt; int();</code></pre>

<p>This function is effectivly the same as</p>

<pre><code>    $y = $x -&gt; map(sub { int });</code></pre>

</dd>
<dt id="floor()"><a id="floor"></a>floor()</dt>
<dd>

<p>Round to negative infinity. Rounds each element to negative infinity.</p>

<pre><code>    $y = $x -&gt; floor();</code></pre>

</dd>
<dt id="ceil()"><a id="ceil"></a>ceil()</dt>
<dd>

<p>Round to positive infinity. Rounds each element to positive infinity.</p>

<pre><code>    $y = $x -&gt; int();</code></pre>

</dd>
<dt id="abs()"><a id="abs"></a>abs()</dt>
<dd>

<p>Absolute value. The absolute value of each element.</p>

<pre><code>    $y = $x -&gt; abs();</code></pre>

<p>This is effectivly the same as</p>

<pre><code>    $y = $x -&gt; map(sub { abs });</code></pre>

</dd>
<dt id="sign()"><a id="sign"></a>sign()</dt>
<dd>

<p>Sign function. Apply the sign function to each element.</p>

<pre><code>    $y = $x -&gt; sign();</code></pre>

<p>This is effectivly the same as</p>

<pre><code>    $y = $x -&gt; map(sub { $_ &lt;=&gt; 0 });</code></pre>

</dd>
</dl>

<h3 id="Columnwise-or-rowwise-mathematical-functions"><a id="Columnwise"></a>Columnwise or rowwise mathematical functions</h3>

<p>These method work along each column or row of a matrix. Some of these methods return a matrix with the same size as the invocand matrix. Other methods collapse the dimension, so that, e.g., if the method is applied to the first dimension a <i>p</i>-by-<i>q</i> matrix becomes a 1-by-<i>q</i> matrix, and if applied to the second dimension, it becomes a <i>p</i>-by-1 matrix. Others, like <code><a href="./Math::Matrix.html#diff()">&quot;diff()&quot;</a></code>, reduces the length along the dimension by one, so a <i>p</i>-by-<i>q</i> matrix becomes a (<i>p</i>-1)-by-<i>q</i> or a <i>p</i>-by-(<i>q</i>-1) matrix.</p>

<dl>

<dt id="sum()"><a id="sum"></a>sum()</dt>
<dd>

<p>Sum of elements along various dimensions of a matrix. If the dimension argument is not given, the first non-singleton dimension is used.</p>

<pre><code>    $y = $x -&gt; sum($dim);
    $y = $x -&gt; sum();</code></pre>

</dd>
<dt id="prod()"><a id="prod"></a>prod()</dt>
<dd>

<p>Product of elements along various dimensions of a matrix. If the dimension argument is not given, the first non-singleton dimension is used.</p>

<pre><code>    $y = $x -&gt; prod($dim);
    $y = $x -&gt; prod();</code></pre>

</dd>
<dt id="mean()"><a id="mean"></a>mean()</dt>
<dd>

<p>Mean of elements along various dimensions of a matrix. If the dimension argument is not given, the first non-singleton dimension is used.</p>

<pre><code>    $y = $x -&gt; mean($dim);
    $y = $x -&gt; mean();</code></pre>

</dd>
<dt id="hypot()"><a id="hypot"></a>hypot()</dt>
<dd>

<p>Hypotenuse. Computes the square root of the sum of the square of each element along various dimensions of a matrix. If the dimension argument is not given, the first non-singleton dimension is used.</p>

<pre><code>    $y = $x -&gt; hypot($dim);
    $y = $x -&gt; hypot();</code></pre>

<p>For example,</p>

<pre><code>    $x = Math::Matrix -&gt; new([[3,  4],
                              [5, 12]]);
    $y = $x -&gt; hypot(2);</code></pre>

<p>returns the 2-by-1 matrix</p>

<pre><code>    [  5 ]
    [ 13 ]</code></pre>

</dd>
<dt id="min()"><a id="min"></a>min()</dt>
<dd>

<p>Minimum of elements along various dimensions of a matrix. If the dimension argument is not given, the first non-singleton dimension is used.</p>

<pre><code>    $y = $x -&gt; min($dim);
    $y = $x -&gt; min();</code></pre>

</dd>
<dt id="max()"><a id="max"></a>max()</dt>
<dd>

<p>Maximum of elements along various dimensions of a matrix. If the dimension argument is not given, the first non-singleton dimension is used.</p>

<pre><code>    $y = $x -&gt; max($dim);
    $y = $x -&gt; max();</code></pre>

</dd>
<dt id="median()"><a id="median"></a>median()</dt>
<dd>

<p>Median of elements along various dimensions of a matrix. If the dimension argument is not given, the first non-singleton dimension is used.</p>

<pre><code>    $y = $x -&gt; median($dim);
    $y = $x -&gt; median();</code></pre>

</dd>
<dt id="cumsum()"><a id="cumsum"></a>cumsum()</dt>
<dd>

<p>Returns the cumulative sum along various dimensions of a matrix. If the dimension argument is not given, the first non-singleton dimension is used.</p>

<pre><code>    $y = $x -&gt; cumsum($dim);
    $y = $x -&gt; cumsum();</code></pre>

</dd>
<dt id="cumprod()"><a id="cumprod"></a>cumprod()</dt>
<dd>

<p>Returns the cumulative product along various dimensions of a matrix. If the dimension argument is not given, the first non-singleton dimension is used.</p>

<pre><code>    $y = $x -&gt; cumprod($dim);
    $y = $x -&gt; cumprod();</code></pre>

</dd>
<dt id="cummean()"><a id="cummean"></a>cummean()</dt>
<dd>

<p>Returns the cumulative mean along various dimensions of a matrix. If the dimension argument is not given, the first non-singleton dimension is used.</p>

<pre><code>    $y = $x -&gt; cummean($dim);
    $y = $x -&gt; cummean();</code></pre>

</dd>
<dt id="diff()"><a id="diff"></a>diff()</dt>
<dd>

<p>Returns the differences between adjacent elements. If the dimension argument is not given, the first non-singleton dimension is used.</p>

<pre><code>    $y = $x -&gt; diff($dim);
    $y = $x -&gt; diff();</code></pre>

</dd>
<dt id="vecnorm()"><a id="vecnorm"></a>vecnorm()</dt>
<dd>

<p>Return the <code>$p</code>-norm of the elements of <code>$x</code>. If the dimension argument is not given, the first non-singleton dimension is used.</p>

<pre><code>    $y = $x -&gt; vecnorm($p, $dim);
    $y = $x -&gt; vecnorm($p);
    $y = $x -&gt; vecnorm();</code></pre>

<p>The <code>$p</code>-norm of a vector is defined as the <code>$p</code>th root of the sum of the absolute values fo the elements raised to the <code>$p</code>th power.</p>

</dd>
<dt id="apply()"><a id="apply"></a>apply()</dt>
<dd>

<p>Applies a subroutine to each row or column of a matrix. If the dimension argument is not given, the first non-singleton dimension is used.</p>

<pre><code>    $y = $x -&gt; apply($sub, $dim);
    $y = $x -&gt; apply($sub);</code></pre>

<p>The subroutine is passed a list with all elements in a single column or row.</p>

</dd>
</dl>

<h2 id="Comparison">Comparison</h2>

<h3 id="Matrix-comparison"><a id="Matrix"></a>Matrix comparison</h3>

<p>Methods matrix comparison. These methods return a scalar value.</p>

<dl>

<dt id="meq()"><a id="meq"></a>meq()</dt>
<dd>

<p>Matrix equal to. Returns 1 if two matrices are identical and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; meq($y);</code></pre>

</dd>
<dt id="mne()"><a id="mne"></a>mne()</dt>
<dd>

<p>Matrix not equal to. Returns 1 if two matrices are different and 0 otherwise.</p>

<pre><code>    $bool = $x -&gt; mne($y);</code></pre>

</dd>
<dt id="equal()"><a id="equal"></a>equal()</dt>
<dd>

<p>Decide if two matrices are equal. The criterion is, that each pair of elements differs less than $Math::Matrix::eps.</p>

<pre><code>    $bool = $x -&gt; equal($y);</code></pre>

</dd>
</dl>

<h3 id="Scalar-comparison"><a id="Scalar"></a>Scalar comparison</h3>

<p>Each of these methods performs scalar (element by element) comparison and returns a matrix of ones and zeros. Scalar expansion is performed if necessary.</p>

<dl>

<dt id="seq()"><a id="seq"></a>seq()</dt>
<dd>

<p>Scalar equality. Performs scalar (element by element) comparison of two matrices.</p>

<pre><code>    $bool = $x -&gt; seq($y);</code></pre>

</dd>
<dt id="sne()"><a id="sne"></a>sne()</dt>
<dd>

<p>Scalar (element by element) not equal to. Performs scalar (element by element) comparison of two matrices.</p>

<pre><code>    $bool = $x -&gt; sne($y);</code></pre>

</dd>
<dt id="slt()"><a id="slt"></a>slt()</dt>
<dd>

<p>Scalar (element by element) less than. Performs scalar (element by element) comparison of two matrices.</p>

<pre><code>    $bool = $x -&gt; slt($y);</code></pre>

</dd>
<dt id="sle()"><a id="sle"></a>sle()</dt>
<dd>

<p>Scalar (element by element) less than or equal to. Performs scalar (element by element) comparison of two matrices.</p>

<pre><code>    $bool = $x -&gt; sle($y);</code></pre>

</dd>
<dt id="sgt()"><a id="sgt"></a>sgt()</dt>
<dd>

<p>Scalar (element by element) greater than. Performs scalar (element by element) comparison of two matrices.</p>

<pre><code>    $bool = $x -&gt; sgt($y);</code></pre>

</dd>
<dt id="sge()"><a id="sge"></a>sge()</dt>
<dd>

<p>Scalar (element by element) greater than or equal to. Performs scalar (element by element) comparison of two matrices.</p>

<pre><code>    $bool = $x -&gt; sge($y);</code></pre>

</dd>
<dt id="scmp()"><a id="scmp"></a>scmp()</dt>
<dd>

<p>Scalar (element by element) comparison. Performs scalar (element by element) comparison of two matrices. Each element in the output matrix is either -1, 0, or 1 depending on whether the elements are less than, equal to, or greater than each other.</p>

<pre><code>    $bool = $x -&gt; scmp($y);</code></pre>

</dd>
</dl>

<h2 id="Vector-functions"><a id="Vector"></a>Vector functions</h2>

<dl>

<dt id="dot_product()"><a id="dot_product"></a>dot_product()</dt>
<dd>

<p>Compute the dot product of two vectors. The second operand does not have to be an object.</p>

<pre><code>    # $x and $y are both objects
    $x = Math::Matrix -&gt; new([1, 2, 3]);
    $y = Math::Matrix -&gt; new([4, 5, 6]);
    $p = $x -&gt; dot_product($y);             # $p = 32

    # Only $x is an object.
    $p = $x -&gt; dot_product([4, 5, 6]);      # $p = 32</code></pre>

</dd>
<dt id="outer_product()"><a id="outer_product"></a>outer_product()</dt>
<dd>

<p>Compute the outer product of two vectors. The second operand does not have to be an object.</p>

<pre><code>    # $x and $y are both objects
    $x = Math::Matrix -&gt; new([1, 2, 3]);
    $y = Math::Matrix -&gt; new([4, 5, 6, 7]);
    $p = $x -&gt; outer_product($y);

    # Only $x is an object.
    $p = $x -&gt; outer_product([4, 5, 6, y]);</code></pre>

</dd>
<dt id="absolute()"><a id="absolute"></a>absolute()</dt>
<dd>

<p>Compute the absolute value (i.e., length) of a vector.</p>

<pre><code>    $v = Math::Matrix -&gt; new([3, 4]);
    $a = $v -&gt; absolute();                  # $v = 5</code></pre>

</dd>
<dt id="normalize()"><a id="normalize"></a>normalize()</dt>
<dd>

<p>Normalize a vector, i.e., scale a vector so its length becomes 1.</p>

<pre><code>    $v = Math::Matrix -&gt; new([3, 4]);
    $u = $v -&gt; normalize();                 # $u = [ 0.6, 0.8 ]</code></pre>

</dd>
<dt id="cross_product()"><a id="cross_product"></a>cross_product()</dt>
<dd>

<p>Compute the cross-product of vectors.</p>

<pre><code>    $x = Math::Matrix -&gt; new([1,3,2],
                             [5,4,2]);
    $p = $x -&gt; cross_product();             # $p = [ -2, 8, -11 ]</code></pre>

</dd>
</dl>

<h2 id="Conversion">Conversion</h2>

<dl>

<dt id="as_string()"><a id="as_string"></a>as_string()</dt>
<dd>

<p>Creates a string representation of the matrix and returns it.</p>

<pre><code>    $x = Math::Matrix -&gt; new([1, 2], [3, 4]);
    $s = $x -&gt; as_string();</code></pre>

</dd>
<dt id="as_array()"><a id="as_array"></a>as_array()</dt>
<dd>

<p>Returns the matrix as an unblessed Perl array, i.e., and ordinary, unblessed reference.</p>

<pre><code>    $y = $x -&gt; as_array();      # ref($y) returns &#39;ARRAY&#39;</code></pre>

</dd>
</dl>

<h2 id="Matrix-utilities"><a id="Matrix1"></a>Matrix utilities</h2>

<h3 id="Apply-a-subroutine-to-each-element"><a id="Apply"></a>Apply a subroutine to each element</h3>

<dl>

<dt id="map()"><a id="map"></a>map()</dt>
<dd>

<p>Call a subroutine for every element of a matrix, locally setting <code>$_</code> to each element and passing the matrix row and column indices as input arguments.</p>

<pre><code>    # square each element
    $y = $x -&gt; map(sub { $_ ** 2 });

    # set strictly lower triangular part to zero
    $y = $x -&gt; map(sub { $_[0] &gt; $_[1] ? 0 : $_ })&#39;</code></pre>

</dd>
<dt id="sapply()"><a id="sapply"></a>sapply()</dt>
<dd>

<p>Applies a subroutine to each element of a matrix, or each set of corresponding elements if multiple matrices are given, and returns the result. The first argument is the subroutine to apply. The following arguments, if any, are additional matrices on which to apply the subroutine.</p>

<pre><code>    $w = $x -&gt; sapply($sub);            # single operand
    $w = $x -&gt; sapply($sub, $y);        # two operands
    $w = $x -&gt; sapply($sub, $y, $z);    # three operands</code></pre>

<p>Each matrix element, or corresponding set of elements, are passed to the subroutine as input arguments.</p>

<p>When used with a single operand, this method is similar to the <code><a href="./Math::Matrix.html#map()">&quot;map()&quot;</a></code> method, the syntax is different, since <code><a href="./Math::Matrix.html#sapply()">&quot;sapply()&quot;</a></code> supports multiple operands.</p>

<p>See also <code><a href="./Math::Matrix.html#map()">&quot;map()&quot;</a></code>.</p>

<ul>

<li><p>The subroutine is run in scalar context.</p>

</li>
<li><p>No checks are done on the return value of the subroutine.</p>

</li>
<li><p>The number of rows in the output matrix equals the number of rows in the operand with the largest number of rows. Ditto for columns. So if <code>$x</code> is 5-by-2 matrix, and <code>$y</code> is a 3-by-4 matrix, the result is a 5-by-4 matrix.</p>

</li>
<li><p>For each operand that has a number of rows smaller than the maximum value, the rows are recyled. Ditto for columns.</p>

</li>
<li><p>Don&#39;t modify the variables $_[0], $_[1] etc. inside the subroutine. Otherwise, there is a risk of modifying the operand matrices.</p>

</li>
<li><p>If the matrix elements are objects that are not cloned when the &quot;=&quot; (assignment) operator is used, you might have to explicitly clone the objects used inside the subroutine. Otherwise, the elements in the output matrix might be references to objects in the operand matrices, rather than references to new objects.</p>

</li>
</ul>

<p>Some examples</p>

<dl>

<dt id="One-operand"><a id="One"></a>One operand</dt>
<dd>

<p>With one operand, i.e., the invocand matrix, the subroutine is applied to each element of the invocand matrix. The returned matrix has the same size as the invocand. For example, multiplying the matrix <code>$x</code> with the scalar <code>$c</code></p>

<pre><code>    $sub = sub { $c * $_[0] };      # subroutine to multiply by $c
    $z = $x -&gt; sapply($sub);        # multiply each element by $c</code></pre>

</dd>
<dt id="Two-operands"><a id="Two"></a>Two operands</dt>
<dd>

<p>When two operands are specfied, the subroutine is applied to each pair of corresponding elements in the two operands. For example, adding two matrices can be implemented as</p>

<pre><code>    $sub = sub { $_[0] * $_[1] };
    $z = $x -&gt; sapply($sub, $y);</code></pre>

<p>Note that if the matrices have different sizes, the rows and/or columns of the smaller are recycled to match the size of the larger. If <code>$x</code> is a <code>$p</code>-by-<code>$q</code> matrix and <code>$y</code> is a <code>$r</code>-by-<code>$s</code> matrix, then <code>$z</code> is a max(<code>$p</code>,<code>$r</code>)-by-max(<code>$q</code>,<code>$s</code>) matrix, and</p>

<pre><code>    $z -&gt; [$i][$j] = $sub -&gt; ($x -&gt; [$i % $p][$j % $q],
                              $y -&gt; [$i % $r][$j % $s]);</code></pre>

<p>Because of this recycling, multiplying the matrix <code>$x</code> with the scalar <code>$c</code> (see above) can also be implemented as</p>

<pre><code>    $sub = sub { $_[0] * $_[1] };
    $z = $x -&gt; sapply($sub, $c);</code></pre>

<p>Generating a matrix with all combinations of <code>$x**$y</code> for <code>$x</code> being 4, 5, and 6 and <code>$y</code> being 1, 2, 3, and 4 can be done with</p>

<pre><code>    $c = Math::Matrix -&gt; new([[4], [5], [6]]);      # 3-by-1 column
    $r = Math::Matrix -&gt; new([[1, 2, 3, 4]]);       # 1-by-4 row
    $x = $c -&gt; sapply(sub { $_[0] ** $_[1] }, $r);  # 3-by-4 matrix</code></pre>

</dd>
<dt id="Multiple-operands"><a id="Multiple"></a>Multiple operands</dt>
<dd>

<p>In general, the sapply() method can have any number of arguments. For example, to compute the sum of the four matrices <code>$x</code>, <code>$y</code>, <code>$z</code>, and <code>$w</code>,</p>

<pre><code>    $sub = sub {
               $sum = 0;
               for $val (@_) {
                   $sum += $val;
               };
               return $sum;
           };
    $sum = $x -&gt; sapply($sub, $y, $z, $w);</code></pre>

</dd>
</dl>

</dd>
</dl>

<h3 id="Forward-elimination"><a id="Forward"></a>Forward elimination</h3>

<p>These methods take a matrix as input, performs forward elimination, and returns a matrix where all elements below the main diagonal are zero. In list context, four additional arguments are returned: an array with the row permutations, an array with the column permutations, an integer with the number of row swaps and an integer with the number of column swaps performed during elimination.</p>

<p>The permutation vectors can be converted to permutation matrices with <code><a href="./Math::Matrix.html#to_permmat()">&quot;to_permmat()&quot;</a></code>.</p>

<dl>

<dt id="felim_np()"><a id="felim_np"></a>felim_np()</dt>
<dd>

<p>Perform forward elimination with no pivoting.</p>

<pre><code>    $y = $x -&gt; felim_np();</code></pre>

<p>Forward elimination without pivoting may fail even when the matrix is non-singular.</p>

<p>This method is provided mostly for illustration purposes.</p>

</dd>
<dt id="felim_tp()"><a id="felim_tp"></a>felim_tp()</dt>
<dd>

<p>Perform forward elimination with trivial pivoting, a variant of partial pivoting.</p>

<pre><code>    $y = $x -&gt; felim_tp();</code></pre>

<p>If A is a p-by-q matrix, and the so far remaining unreduced submatrix starts at element (i,i), the pivot element is the first element in column i that is non-zero.</p>

<p>This method is provided mostly for illustration purposes.</p>

</dd>
<dt id="felim_pp()"><a id="felim_pp"></a>felim_pp()</dt>
<dd>

<p>Perform forward elimination with (unscaled) partial pivoting.</p>

<pre><code>    $y = $x -&gt; felim_pp();</code></pre>

<p>If A is a p-by-q matrix, and the so far remaining unreduced submatrix starts at element (i,i), the pivot element is the element in column i that has the largest absolute value.</p>

<p>This method is provided mostly for illustration purposes.</p>

</dd>
<dt id="felim_sp()"><a id="felim_sp"></a>felim_sp()</dt>
<dd>

<p>Perform forward elimination with scaled pivoting, a variant of partial pivoting.</p>

<pre><code>    $y = $x -&gt; felim_sp();</code></pre>

<p>If A is a p-by-q matrix, and the so far remaining unreduced submatrix starts at element (i,i), the pivot element is the element in column i that has the largest absolute value relative to the other elements on the same row.</p>

</dd>
<dt id="felim_fp()"><a id="felim_fp"></a>felim_fp()</dt>
<dd>

<p>Performs forward elimination with full pivoting.</p>

<pre><code>    $y = $x -&gt; felim_fp();</code></pre>

<p>The elimination is done with full pivoting, also called complete pivoting or total pivoting. If A is a p-by-q matrix, and the so far remaining unreduced submatrix starts at element (i,i), the pivot element is the element in the whole submatrix that has the largest absolute value. With full pivoting, both rows and columns might be swapped.</p>

</dd>
</dl>

<h3 id="Back-substitution"><a id="Back"></a>Back-substitution</h3>

<dl>

<dt id="bsubs()"><a id="bsubs"></a>bsubs()</dt>
<dd>

<p>Performs back-substitution.</p>

<pre><code>    $y = $x -&gt; bsubs();</code></pre>

<p>The leftmost square portion of the matrix must be upper triangular.</p>

</dd>
</dl>

<h2 id="Miscellaneous-methods"><a id="Miscellaneous1"></a>Miscellaneous methods</h2>

<dl>

<dt id="print()"><a id="print"></a>print()</dt>
<dd>

<p>Prints the matrix on STDOUT. If the method has additional parameters, these are printed before the matrix is printed.</p>

</dd>
<dt id="version()"><a id="version"></a>version()</dt>
<dd>

<p>Returns a string contining the package name and version number.</p>

</dd>
</dl>

<h1 id="OVERLOADING">OVERLOADING</h1>

<p>The following operators are overloaded.</p>

<dl>

<dt id="+-and-+="><a id="and"></a><code>+</code> and <code>+=</code></dt>
<dd>

<p>Matrix or scalar addition. Unless one or both of the operands is a scalar, both operands must have the same size.</p>

<pre><code>    $C  = $A + $B;      # assign $A + $B to $C
    $A += $B;           # assign $A + $B to $A</code></pre>

<p>Note that</p>

</dd>
<dt id="-and-="><a id="and1"></a><code>-</code> and <code>-=</code></dt>
<dd>

<p>Matrix or scalar subtraction. Unless one or both of the operands is a scalar, both operands must have the same size.</p>

<pre><code>    $C  = $A + $B;      # assign $A - $B to $C
    $A += $B;           # assign $A - $B to $A</code></pre>

</dd>
<dt id="*-and-*="><a id="and2"></a><code>*</code> and <code>*=</code></dt>
<dd>

<p>Matrix or scalar multiplication. Unless one or both of the operands is a scalar, the number of columns in the first operand must be equal to the number of rows in the second operand.</p>

<pre><code>    $C  = $A * $B;      # assign $A * $B to $C
    $A *= $B;           # assign $A * $B to $A</code></pre>

</dd>
<dt id="**-and-**="><a id="and3"></a><code>**</code> and <code>**=</code></dt>
<dd>

<p>Matrix power. The second operand must be a scalar.</p>

<pre><code>    $C  = $A * $B;      # assign $A ** $B to $C
    $A *= $B;           # assign $A ** $B to $A</code></pre>

</dd>
<dt id="=="><a id="pod"></a><code>==</code></dt>
<dd>

<p>Equal to.</p>

<pre><code>    $A == $B;           # is $A equal to $B?</code></pre>

</dd>
<dt id="!="><a id="pod1"></a><code>!=</code></dt>
<dd>

<p>Not equal to.</p>

<pre><code>    $A != $B;           # is $A not equal to $B?</code></pre>

</dd>
<dt id="neg1"><code>neg</code></dt>
<dd>

<p>Negation.</p>

<pre><code>    $B = -$A;           # $B is the negative of $A</code></pre>

</dd>
<dt id="~"><a id="pod2"></a><code>~</code></dt>
<dd>

<p>Transpose.</p>

<pre><code>    $B = ~$A;           # $B is the transpose of $A</code></pre>

</dd>
<dt id="abs1"><code>abs</code></dt>
<dd>

<p>Absolute value.</p>

<pre><code>    $B = abs $A;        # $B contains absolute values of $A</code></pre>

</dd>
<dt id="int1"><code>int</code></dt>
<dd>

<p>Truncate to integer.</p>

<pre><code>    $B = int $A;        # $B contains only integers</code></pre>

</dd>
</dl>

<h1 id="IMPROVING-THE-SOLUTION-OF-LINEAR-SYSTEMS"><a id="IMPROVING"></a>IMPROVING THE SOLUTION OF LINEAR SYSTEMS</h1>

<p>The methods that do an explicit or implicit matrix left division accept some additional parameters. If these parameters are specified, the matrix left division is done repeatedly in an iterative way, which often gives a better solution.</p>

<h2 id="Background">Background</h2>

<p>The linear system of equations</p>

<pre><code>    $A * $x = $y</code></pre>

<p>can be solved for <code>$x</code> with</p>

<pre><code>    $x = $y -&gt; mldiv($A);</code></pre>

<p>Ideally <code>$A * $x</code> should equal <code>$y</code>, but due to numerical errors, this is not always the case. The following illustrates how to improve the solution <code>$x</code> computed above:</p>

<pre><code>    $r = $A -&gt; mmuladd($x, -$y);    # compute the residual $A*$x-$y
    $d = $r -&gt; mldiv($A);           # compute the delta for $x
    $x -= $d;                       # improve the solution $x</code></pre>

<p>This procedure is repeated, and at each step, the absolute error</p>

<pre><code>    ||$A*$x - $y|| = ||$r||</code></pre>

<p>and the relative error</p>

<pre><code>    ||$A*$x - $y|| / ||$y|| = ||$r|| / ||$y||</code></pre>

<p>are computed and compared to the tolerances. Once one of the stopping criteria is satisfied, the algorithm terminates.</p>

<h2 id="Stopping-criteria"><a id="Stopping"></a>Stopping criteria</h2>

<p>The algorithm stops when at least one of the errors are within the specified tolerances or the maximum number of iterations is reached. If the maximum number of iterations is reached, but noen of the errors are within the tolerances, a warning is displayed and the best solution so far is returned.</p>

<h2 id="Parameters">Parameters</h2>

<dl>

<dt id="MaxIter">MaxIter</dt>
<dd>

<p>The maximum number of iterations to perform. The value must be a positive integer. The default is 20.</p>

</dd>
<dt id="RelTol">RelTol</dt>
<dd>

<p>The limit for the relative error. The value must be a non-negative. The default value is 1e-19 when perl is compiled with long doubles or quadruple precision, and 1e-9 otherwise.</p>

</dd>
<dt id="AbsTol">AbsTol</dt>
<dd>

<p>The limit for the absolute error. The value must be a non-negative. The default value is 0.</p>

</dd>
<dt id="Debug">Debug</dt>
<dd>

<p>If this parameter does not affect when the algorithm terminates, but when set to non-zero, some information is displayed at each step.</p>

</dd>
</dl>

<h2 id="Example">Example</h2>

<p>If</p>

<pre><code>    $A = [[  8, -8, -5,  6, -1,  3 ],
          [ -7, -1,  5, -9,  5,  6 ],
          [ -7,  8,  9, -2, -4,  3 ],
          [  3, -4,  5,  5,  3,  3 ],
          [  9,  8, -3, -4,  1,  6 ],
          [ -8,  9, -1,  3,  5,  2 ]];

    $y = [[  80, -13 ],
          [  -2, 104 ],
          [ -57, -27 ],
          [  47, -28 ],
          [   5,  77 ],
          [  91, 133 ]];</code></pre>

<p>the result of <code>$x = $y -&gt; mldiv($A);</code>, using double precision arithmetic, is the approximate solution</p>

<pre><code>    $x = [[ -2.999999999999998, -5.000000000000000 ],
          [ -1.000000000000000,  3.000000000000001 ],
          [ -5.999999999999997, -8.999999999999996 ],
          [  8.000000000000000, -2.000000000000003 ],
          [  6.000000000000003,  9.000000000000002 ],
          [  7.999999999999997,  8.999999999999995 ]];</code></pre>

<p>The residual <code>$res = $A -&gt; mmuladd($x, -$y);</code> is</p>

<pre><code>    $res = [[  1.24344978758018e-14,  1.77635683940025e-15 ],
            [  8.88178419700125e-15, -5.32907051820075e-15 ],
            [ -1.24344978758018e-14,  1.77635683940025e-15 ],
            [ -7.10542735760100e-15, -4.08562073062058e-14 ],
            [ -1.77635683940025e-14, -3.81916720471054e-14 ],
            [  1.24344978758018e-14,  8.43769498715119e-15 ]];</code></pre>

<p>and the delta <code>$dx = $res -&gt; mldiv($A);</code> is</p>

<pre><code>    $dx = [[   -8.592098303124e-16, -2.86724066474914e-15 ],
           [ -7.92220125658508e-16, -2.99693950082398e-15 ],
           [ -2.22533360993874e-16,  3.03465504177947e-16 ],
           [  6.47376093198353e-17, -1.12378127899388e-15 ],
           [  6.35204502123966e-16,  2.40938179521241e-15 ],
           [  1.55166908001001e-15,  2.08339859425849e-15 ]];</code></pre>

<p>giving the improved, and in this case exact, solution <code>$x -= $dx;</code>,</p>

<pre><code>    $x = [[ -3, -5 ],
          [ -1,  3 ],
          [ -6, -9 ],
          [  8, -2 ],
          [  6,  9 ],
          [  8,  9 ]];</code></pre>

<h1 id="SUBCLASSING">SUBCLASSING</h1>

<p>The methods should work fine with any kind of numerical objects, provided that the assignment operator <code>=</code> is overloaded, so that Perl knows how to create a copy.</p>

<p>You can check the behaviour of the assignment operator by assigning a value to a new variable, modify the new variable, and check whether this also modifies the original value. Here is an example:</p>

<pre><code>    $x = Some::Class -&gt; new(0);           # create object $x
    $y = $x;                              # create new variable $y
    $y++;                                 # modify $y
    print &quot;it&#39;s a clone\n&quot; if $x != $y;   # is $x modified?</code></pre>

<p>The subclass might need to implement some methods of its own. For instance, if each element is a complex number, a transpose() method needs to be implemented to take the complex conjugate of each value. An as_string() method might also be useful for displaying the matrix in a format more suitable for the subclass.</p>

<p>Here is an example showing Math::Matrix::Complex, a fully-working subclass of Math::Matrix, where each element is a Math::Complex object.</p>

<pre><code>    use strict;
    use warnings;

    package Math::Matrix::Complex;

    use Math::Matrix;
    use Scalar::Util &#39;blessed&#39;;
    use Math::Complex 1.57;     # &quot;=&quot; didn&#39;t clone before 1.57

    our @ISA = (&#39;Math::Matrix&#39;);

    # We need a new() method to make sure every element is an object.

    sub new {
        my $self = shift;
        my $x = $self -&gt; SUPER::new(@_);

        my $sub = sub {
            defined(blessed($_[0])) &amp;&amp; $_[0] -&gt; isa(&#39;Math::Complex&#39;)
              ? $_[0]
              : Math::Complex -&gt; new($_[0]);
        };

        return $x -&gt; sapply($sub);
    }

    # We need a transpose() method, since the transpose of a matrix
    # with complex numbers also takes the conjugate of all elements.

    sub transpose {
        my $x = shift;
        my $y = $x -&gt; SUPER::transpose(@_);

        return $y -&gt; sapply(sub { ~$_[0] });
    }

    # We need an as_string() method, since our parent&#39;s methods
    # doesn&#39;t format complex numbers correctly.

    sub as_string {
        my $self = shift;
        my $out = &quot;&quot;;
        for my $row (@$self) {
            for my $elm (@$row) {
                $out = $out . sprintf &quot;%10s &quot;, $elm;
            }
            $out = $out . sprintf &quot;\n&quot;;
        }
        $out;
    }

    1;</code></pre>

<h1 id="BUGS">BUGS</h1>

<p>Please report any bugs or feature requests via <a href="https://github.com/pjacklam/p5-Math-Matrix/issues">https://github.com/pjacklam/p5-Math-Matrix/issues</a>.</p>

<p>Old bug reports and feature requests can be found at <a href="http://rt.cpan.org/NoAuth/Bugs.html?Dist=Math-Matrix">http://rt.cpan.org/NoAuth/Bugs.html?Dist=Math-Matrix</a>.</p>

<h1 id="SUPPORT">SUPPORT</h1>

<p>You can find documentation for this module with the perldoc command.</p>

<pre><code>    perldoc Math::Matrix</code></pre>

<p>You can also look for information at:</p>

<ul>

<li><p>GitHub Source Repository</p>

<p><a href="https://github.com/pjacklam/p5-Math-Matrix">https://github.com/pjacklam/p5-Math-Matrix</a></p>

</li>
<li><p>MetaCPAN</p>

<p><a href="https://metacpan.org/release/Math-Matrix">https://metacpan.org/release/Math-Matrix</a></p>

</li>
<li><p>CPAN Ratings</p>

<p><a href="http://cpanratings.perl.org/d/Math-Matrix">http://cpanratings.perl.org/d/Math-Matrix</a></p>

</li>
<li><p>CPAN Testers PASS Matrix</p>

<p><a href="http://pass.cpantesters.org/distro/A/Math-Matrix.html">http://pass.cpantesters.org/distro/A/Math-Matrix.html</a></p>

</li>
<li><p>CPAN Testers Reports</p>

<p><a href="http://www.cpantesters.org/distro/A/Math-Matrix.html">http://www.cpantesters.org/distro/A/Math-Matrix.html</a></p>

</li>
<li><p>CPAN Testers Matrix</p>

<p><a href="http://matrix.cpantesters.org/?dist=Math-Matrix">http://matrix.cpantesters.org/?dist=Math-Matrix</a></p>

</li>
</ul>

<h1 id="LICENSE-AND-COPYRIGHT"><a id="LICENSE"></a>LICENSE AND COPYRIGHT</h1>

<p>Copyright (c) 2020-2021, Peter John Acklam</p>

<p>Copyright (C) 2013, John M. Gamble &lt;jgamble@ripco.com&gt;, all rights reserved.</p>

<p>Copyright (C) 2009, oshalla https://rt.cpan.org/Public/Bug/Display.html?id=42919</p>

<p>Copyright (C) 2002, Bill Denney &lt;gte273i@prism.gatech.edu&gt;, all rights reserved.</p>

<p>Copyright (C) 2001, Brian J. Watson &lt;bjbrew@power.net&gt;, all rights reserved.</p>

<p>Copyright (C) 2001, Ulrich Pfeifer &lt;pfeifer@wait.de&gt;, all rights reserved. Copyright (C) 1995, Universit&auml;t Dortmund, all rights reserved.</p>

<p>Copyright (C) 2001, Matthew Brett &lt;matthew.brett@mrc-cbu.cam.ac.uk&gt;</p>

<p>This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.</p>

<h1 id="AUTHORS">AUTHORS</h1>

<p>Peter John Acklam &lt;pjacklam@gmail.com&gt; (2020-2021)</p>

<p>Ulrich Pfeifer &lt;pfeifer@ls6.informatik.uni-dortmund.de&gt; (1995-2013)</p>

<p>Brian J. Watson &lt;bjbrew@power.net&gt;</p>

<p>Matthew Brett &lt;matthew.brett@mrc-cbu.cam.ac.uk&gt;</p></div>

<div id="metacpan_install-instructions-dialog" class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">Module Install Instructions</h4>
      </div>
      <div class="modal-body">
        <p>To install Math::Matrix, copy and paste the appropriate command in to your terminal.</p>
        <p><a href="https://metacpan.org/dist/App-cpanminus/view/bin/cpanm">cpanm</a></p>
        <pre><code>cpanm Math::Matrix</code></pre>
        <p><a href="https://metacpan.org/pod/CPAN">CPAN shell</a></p>
        <pre><code>perl -MCPAN -e shell
install Math::Matrix</code></pre>
        <p>For more information on module installation, please visit <a href="https://www.cpan.org/modules/INSTALL.html">the detailed CPAN module installation guide</a>.</p>
      </div>
      <div class="modal-footer">
        <a href="./Math::Matrix.html#" data-dismiss="modal" class="btn">Close</a>
      </div>
    </div>
  </div>
</div>
          </main>
          <div class="content-pagination">
          </div>
        </div>
        <footer class="footer">
          <div class="footer-container">
            <div class="footer-social">
              <div class="footer-link footer-logo">
                <a href="https://metacpan.org/">
                  <img src="https://metacpan.org/static/images/metacpan-logo.svg" alt="MetaCPAN" />
                </a>
              </div>
              <a class="footer-social-link" href="https://github.com/metacpan">
                <i class="fab fa-github-square"></i>
              </a>
              <a class="footer-social-link" href="https://fosstodon.org/@metacpan">
                <i class="fab fa-mastodon"></i>
              </a>
            </div>
            <div class="footer-links">
              <div class="footer-link">
                  <a href="https://metacpan.org/about">About</a>
              </div>
              <div class="footer-link">
                  <a href="https://metacpan.org/about/sponsors">Sponsor</a>
              </div>
              <div class="footer-link">
                  <a href="https://grep.metacpan.org">grep::cpan</a>
              </div>
              <div class="footer-link">
                  <a href="https://metacpan.org/recent">Recent</a>
              </div>
              <div class="footer-link">
                  <a href="https://metacpan.org/about/faq">FAQ</a>
              </div>
              <div class="footer-link">
                  <a href="https://metacpan.org/tools">Tools</a>
              </div>
              <div class="footer-link">
                  <a href="https://fastapi.metacpan.org/">API</a>
              </div>
              <div class="footer-link">
                  <a href="https://www.perl.org/">Perl.org</a>
              </div>
            </div>
            <div class="footer-sponsors">
              <a class="footer-sponsor-link" target="_blank" href="https://www.bytemark.co.uk/" rel="noopener">
                <img class="footer-sponsor-bytemark" src="https://metacpan.org/static/images/sponsors/bytemark_logo.svg" alt="Bytemark logo">
              </a>
              <a class="footer-sponsor-link" target="_blank" href="https://www.liquidweb.com/" rel="noopener">
                <img class="footer-sponsor-liquidweb" src="https://metacpan.org/static/images/sponsors/liquidweb_logo.png" alt="liquidweb logo">
              </a>
              <a class="footer-sponsor-link" target="_blank" href="https://deriv.com/careers/" rel="noopener">
                <img class="footer-sponsor-deriv" src="https://metacpan.org/static/images/sponsors/deriv.svg" alt="Deriv logo">
              </a>
              <a class="footer-sponsor-link" target="_blank" href="https://geocode.xyz" rel="noopener">
                <img class="footer-sponsor-geocode" src="https://metacpan.org/static/images/sponsors/geocodelogo.svg" alt="Geocode logo">
              </a>
              <a class="footer-sponsor-link" target="_blank" href="https://www.fastly.com/" rel="noopener">
                <img class="footer-sponsor-fastly" src="https://metacpan.org/static/images/sponsors/fastly_logo.svg" alt="Fastly logo">
              </a>
              <a class="footer-sponsor-link" target="_blank" href="https://opencagedata.com" rel="noopener">
                <img class="footer-sponsor-opencage" src="https://metacpan.org/static/images/sponsors/open-cage.svg" alt="OpenCage logo">
              </a>
            </div>
          </div>
        </footer>
        <div class="modal fade" tabindex="-1" role="dialog" id="metacpan_keyboard-shortcuts">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Keyboard Shortcuts</h4>
              </div>
              <div class="modal-body row">
<div class="col-md-6">
  <table class="table keyboard-shortcuts">
    <thead>
      <tr>
        <th></th>
        <th>Global</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="keys">
          <kbd>s</kbd>
        </td>
        <td>Focus search bar</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>?</kbd>
        </td>
        <td>Bring up this help dialog</td>
      </tr>
    </tbody>
  </table>

  <table class="table keyboard-shortcuts">
    <thead>
      <tr>
        <th></th>
        <th>GitHub</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>p</kbd>
        </td>
        <td>Go to pull requests</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>i</kbd>
        </td>
        <td>go to github issues (only if github is preferred repository)</td>
      </tr>
    </tbody>
  </table>
</div>

<div class="col-md-6">
  <table class="table keyboard-shortcuts">
    <thead>
      <tr>
        <th></th>
        <th>POD</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>a</kbd>
        </td>
        <td>Go to author</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>c</kbd>
        </td>
        <td>Go to changes</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>i</kbd>
        </td>
        <td>Go to issues</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>d</kbd>
        </td>
        <td>Go to dist</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>r</kbd>
        </td>
        <td>Go to repository/SCM</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>s</kbd>
        </td>
        <td>Go to source</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>b</kbd>
        </td>
        <td>Go to file browse</td>
      </tr>

    </tbody>
  </table>
</div>

<div class="col-md-12">
  <table class="table keyboard-shortcuts">
    <thead>
      <tr>
        <th></th>
        <th>Search terms</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><em>module:</em> (e.g. <a href="https://metacpan.org/search?q=module%3APlugin">module:Plugin</a>)</td>
      </tr>
      <tr>
        <td><em>distribution:</em> (e.g. <a href="https://metacpan.org/search?q=distribution%3ADancer+auth">distribution:Dancer auth</a>)</td>
      </tr>
      <tr>
        <td><em>author:</em> (e.g. <a href="https://metacpan.org/search?q=author%3ASONGMU+Redis">author:SONGMU Redis</a>)</td>
      </tr>
      <tr>
        <td><em>version:</em> (e.g. <a href="https://metacpan.org/search?q=version%3A1.00">version:1.00</a>)</td>
      </tr>
    </tbody>
  </table>
</div>
              </div>
              <div class="modal-footer"></div>
            </div>
          </div>
        </div>
    </body>
</html>
